Accessing Vuex state when defining Vue-Router routes

后端 未结 6 1941
野的像风
野的像风 2020-11-27 15:53

I have the following Vuex store (main.js):

import Vue from \'vue\'
import Vuex from \'vuex\'

Vue.use(Vuex)

//init sto         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 16:07

    Managing your location state separate from the rest of your application state can make things like this harder than they maybe need to be. After dealing with similar problems in both Redux and Vuex, I started managing my location state inside my Vuex store, using a router module. You might want to think about using that approach.

    In your specific case, you could watch for when the location changes within the Vuex store itself, and dispatch the appropriate "redirect" action, like this:

    dispatch("router/push", {path: "/login"})
    

    It's easier than you might think to manage the location state as a Vuex module. You can use mine as a starting point if you want to try it out:

    https://github.com/geekytime/vuex-router

提交回复
热议问题