We usually fetch vuex data in vue using this.$store. However, in vuue -router routing guard, this does not refer to vue instance. Obtain the vuex data from the routing guard as follows: 1. Import an instance of vuex.
import vuex from './store/index';
Copy the code
2. Obtain data from vuex by importing vuex instance. State in routing guard:
router.beforeEach( (to, from, next) = > {
if(from.path === '/preview') {// vuex.state === this.$store.state
console.log(vuex.state.stage)
next()
})
Copy the code
This allows the vuex value to be retrieved from the route guard.