vue-router-如何获取上一页网址?

我想在中获取上一页链接或URL vue-router像这样 怎么做?

const link = this.$router.getPrevLink(); // function is not exist?
this.$router.push(link);

近概念是this.$router.go(-1)

this.$router.go(-1);
Green小宇宙伽罗2020/03/13 15:39:49

所有vue-router的导航卫士都将前一条路线作为from参数接收..

每个保护函数都接收三个参数:

  • to: Route:被导航到的目标Route对象。

  • from: Route:正在远离的当前路线。

  • next: Function: this function must be called to resolve the hook. The action depends on the arguments provided to next

As an example you could use beforeRouteEnter, an in-component navigation guard, to get the previous route and store it in your data ..

...
data() {
 return {
   ...
   prevRoute: null
 }
},
beforeRouteEnter(to, from, next) {
  next(vm => {
    vm.prevRoute = from
  })
},
...

Then you can use this.prevRoute.path to get the previous URL.