如何获取上一页网址

如何获取上一页网址

本文介绍了vue-router - 如何获取上一页网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get previous page link or url in vue-router. Like a this. How to do it?

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

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

this.$router.go(-1);

推荐答案

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

All of vue-router's navigation guards receive the previous route as a from argument ..

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

  • to: Route:导航到的目标路由对象.

  • to: Route: the target Route Object being navigated to.

from: Route:当前导航离开的路线.

from: Route: the current route being navigated away from.

next: Function:必须调用这个函数来解析钩子.这action 取决于提供给 next 的参数

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

举个例子,你可以使用 beforeRouteEnter,一个组件内导航守卫,来获取之前的路线并将其存储在你的数据中..

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
  })
},
...

然后就可以使用this.prevRoute.path来获取之前的URL.

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

这篇关于vue-router - 如何获取上一页网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 15:55