【问题标题】:Call vuex getters from Vue-Router从 Vue-Router 调用 vuex getter
【发布时间】:2016-08-10 20:35:46
【问题描述】:

是否可以从路由器调用 vuex getter? 我需要从 beforeach 钩子中检查布尔变量的状态:

router.beforeEach(function ({ to, next }) {
    if (to.path !== '/login') {
        // return a Promise that resolves to true or false
        return the-needed-variable-from-store;
    } else {
        next()
    }
})
如何获取和检查从商店派生的变量?

非常感谢!

【问题讨论】:

  • 只需导入存储路由器文件 "import store from '../store'" 并返回存储值 "return store.getters" 但是如果你想访问存储的值路由,最好使用 vue-router-sync 包

标签: reactive-programming vue.js vue-router vue-component


【解决方案1】:

这里有一个讨论:https://github.com/vuejs/vuex-router-sync/issues/3#issuecomment-200192468

我能够以一种相当荒谬的方式访问该变量:

router.beforeEach((transition)=>{
    if(transition.to.auth){
        if(!transition.to.router.app.$store.state.auth.authorized){
            transition.abort()
            router.go({path:"/"})
        }
    }
    transition.next()
});

所以在你的情况下,它可能看起来像 to.router.app.$store.state

【讨论】:

    猜你喜欢
    • 2019-04-11
    • 2019-10-19
    • 2023-03-20
    • 2019-02-02
    • 2023-03-06
    • 2020-05-01
    • 2021-04-26
    • 2018-02-22
    • 2017-12-18
    相关资源
    最近更新 更多