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

vue.js Vue.js

米亚西门

2020-03-13

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

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

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

this.$router.go(-1);

第1407篇《vue-router-如何获取上一页网址?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
Green小宇宙伽罗 2020.03.13

所有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.

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android