vue-router

router.js

var router = new Router({
  routes: [{
      path: '/landing/:code',
      redirect: '/order/newOrders'
  }]
})

router.beforeEach((to, from, next) => {
  // 如何在这里获取到code值
})

当访问http://xxx.xxx.com/landing/fs... 路由会跳转到http://xxx.xxx.com/order/newO...,此时在beforeEach里获取到的to对象就是的params就没有code的值了 请问如何保留code值

阅读 2.7k
3 个回答

在from里获取试试

每个钩子方法接收三个参数:

to: Route: 即将要进入的目标 路由对象

from: Route: 当前导航正要离开的路由

this.$route.params.code this取当前的

我用的是Vue 1.x,不知道这个是不是影响参数结构。Vue 1.x的beforeEach只接受一个参数,from和to和next都在此参数里。
参见传入参数的结构:
clipboard.png

代码:

router.beforeEach(function(transition) {
    console.log('transition-------------', transition);
    // transition.to.params表示to接受到的数据,参见上图
    transition.next();
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题