【问题标题】:Access Vuex State from VueRouter从 VueRouter 访问 Vuex 状态
【发布时间】:2020-07-28 20:18:54
【问题描述】:

我正在尝试围绕 vue.js 的 Navigation Guards。我试图按照这个例子做,但我认为我在将我的状态(store.js)连接到我的路由器(routes.js)方面做得不够。在我的 Login.vue 组件中,我尝试在 loadUser 分派到 State 之后推送到新路由,随后 isLoggedIn 更新为 true。在我使用 mapState 的 vue 组件中,我是否需要在 routes.js 中做同样的事情。或者也许有更好的方法来做到这一点。为了提供完整的图片,我使用了 vue 前端和 laravel 后端以及 Sanctum。

从 routes.js 中提取

const routes = [
    {
        path: '/',
        component: Login,
        name: 'login'
    },
    {
        path: '/dashboard',
        component: Dashboard,
        name: 'dashboard',
        meta: { requiresAuth: true }
    },
]

const router = new VueRouter({
    mode: 'history',
    routes // short for `routes: routes`
});

router.beforeEach((to, from, next) => {
    // if (to.matched.some(record => record.meta.requiresAuth)) {
    if (to.meta.requiresAuth) {
        if (store.state.isLoggedIn) {
            next();
        } else {
            next({
                name: 'home'
            });
        }
    } else {
        next();
    }
});

export default router;

从 store.js 中提取

export default {
    state : {
        isLoggedIn: false,
    }
}

从 Login.vue 中提取

methods : {
        async login() {
            this.loading = true;
            this.errors = null;

            try {
                await axios.get('/sanctum/csrf-cookie');
                await axios.post('/login', this.user);

                logIn();
                this.$store.dispatch('loadUser');
                this.$router.push({ name: 'logbook'});

            } catch (error) {
                this.errors = error.response && error.response.data.errors;
            }

            this.loading = false;
        }
    },

【问题讨论】:

  • 也许我应该将重定向从 vue 组件移动到路由器?

标签: laravel vue.js vuex


【解决方案1】:

如果你想访问/更改VueRouter中的Vuex,你必须使用命令router.app.store.state...。

例如:

const routes = [
  { path: '/', component: Login, name: 'login' },
  { path: '/dashboard', component: Dashboard, name: 'dashboard', meta: { requiresAuth: true } },
];

const router = new VueRouter({
  mode: 'history',
  routes,
});

router.beforeEach((to, from, next) => {
  if (router.app.$store) {
    // Example to access the state
    console.log(router.app.$store.state.isLoggedIn);

    // Example to exec the mutation
    router.app.$store.commit('myMutation', 'myArgs');

    // Example to exec the action (ATTENTION IS ASYNC)
    router.app.$store.dispatch('myAction', 'myArgs');
  }

  if (to.meta.requiresAuth) {
    // ...
  } else {
    next();
  }
});

export default router;

注意: 您必须先加载 Vuex,然后再加载 VueRouter。因为,当 VueRouter 加载时,Vuex 需要准备好。您可以在main.js 中更改此设置:

import Vue from 'vue'
import App from './App.vue'
import store from './store' // FIRST IMPORT
import router from './router' // SECOND IMPORT

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

另外,我更喜欢在 cookie 中控制登录,因为用户可以打开很多标签。这是我的意见,你可以做你喜欢的。

【讨论】:

  • 这个答案解决了你的问题吗?请接受此答案,这将对其他人有所帮助。 How to accept an answer?
猜你喜欢
  • 2021-04-21
  • 1970-01-01
  • 2017-05-12
  • 2021-02-23
  • 2021-05-15
  • 2019-11-17
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多