我想在 vue-router
中获取上一页链接或 url。像这样。怎么做?
const link = this.$router.getPrevLink(); // function is not exist?
this.$router.push(link);
接近的概念是 this.$router.go(-1)
。
this.$router.go(-1);
最佳答案
vue-router 的所有 navigation guards以 from
参数的形式接收之前的路由 ..
Every guard function receives three arguments:
to: Route
: the target Route Object being navigated to.
from: Route
: the current route being navigated away from.
next: Function
: this function must be called to resolve the hook. The action depends on the arguments provided to next
例如,您可以使用组件内导航守卫 beforeRouteEnter
来获取之前的路线并将其存储在您的数据中..
...
data() {
return {
...
prevRoute: null
}
},
beforeRouteEnter(to, from, next) {
next(vm => {
vm.prevRoute = from
})
},
...
然后你可以使用this.prevRoute.path
来获取之前的URL。
关于vue.js - vue-router - 如何获取上一页 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53788975/