【问题标题】:vue.js - Redirect depending on a value set on the Vue instancevue.js - 根据 Vue 实例上设置的值重定向
【发布时间】:2018-03-03 16:20:02
【问题描述】:

我正在尝试根据是否在 Vue 实例上设置值来重定向用户。目前,我正在使用这样的Global Route Navigation Guard

这不是一个有效的例子

router.beforeEach((to, from, next) => {
  next(vm => {
    if (!vm.value) {
      vm.value = 123
      return {path: '/url'}
    }
  })
})

我是否需要在 if 语句中调用 next('/url') ?或者有没有更好的有条件重定向的方法。

提前致谢。

【问题讨论】:

  • 类似于this 但是他们没有使用实例。不知道他们是如何得到userNotLoggedIn

标签: javascript vuejs2 vue-router


【解决方案1】:

您可以使用router.app 访问根Vue 实例,如here 所述

router.beforeEach((to, from, next) => {
    if(!router.app.value){
        router.app.value = 123
        next('/url')
        return
    }

    next()
})

【讨论】:

  • 谢谢,我会试一试。我发现了“路由器被注入的根 Vue 实例”。让我觉得这只是最初的Vue() 而不是每个实例。
  • 如果你有多个 Vue 实例,路由状态将被共享。
猜你喜欢
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 2019-08-24
  • 1970-01-01
  • 2018-10-10
  • 2016-05-08
  • 1970-01-01
  • 2017-05-13
相关资源
最近更新 更多