我想在 vue-router
中获取上一页链接或网址。像这样。怎么做?
const link = this.$router.getPrevLink(); // function is not exist?
this.$router.push(link);
近概念是 this.$router.go(-1)
。
this.$router.go(-1);
原文由 narupo 发布,翻译遵循 CC BY-SA 4.0 许可协议
我想在 vue-router
中获取上一页链接或网址。像这样。怎么做?
const link = this.$router.getPrevLink(); // function is not exist?
this.$router.push(link);
近概念是 this.$router.go(-1)
。
this.$router.go(-1);
原文由 narupo 发布,翻译遵循 CC BY-SA 4.0 许可协议
对于带有 Vue Router 4 的 Vue 3,我的解决方案是:
this.$router.options.history.state.back
最终代码:
export default defineComponent({
name: 'Error404',
data () {
return {
lastPath: null
}
},
created () {
this.lastPath = this.$router.options.history.state.back
},
computed: {
prevRoutePatch () {
return this.lastPath ? this.lastPath : '/dashboard'
}
}
})
问候。
原文由 David Becerra Montellano 发布,翻译遵循 CC BY-SA 4.0 许可协议
5 回答4.9k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答4.8k 阅读✓ 已解决
4 回答4.4k 阅读✓ 已解决
4 回答1.9k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
vue-router 的所有 导航守卫 都将先前的路由作为
from
参数接收。例如,您可以使用
beforeRouteEnter
,一个组件内的导航守卫,来获取先前的路线并将其存储在您的数据中..然后就可以使用
this.prevRoute.path
获取之前的URL。