首先确保 main.js 中组合了 vuex 和 router

Vue.use(VueRouter)
Vue.use(Vuex)

new Vue({
  el: '#app',
  router: createVueRouter(VueRouter),
  store: createVueStore(Vuex),
  render: h => h(App)
})

router.js 中可以直接使用 router.app.$options.store

let be4Each = function(router) {
  router.beforeEach((to, from, next) => {
    router.app.$options.store.dispatch('handleDoSomething').then((i) => {
      // doSomething
      next();
    });
  });
}

let createVueRouter = function(VueRouter) {
  let router = new VueRouter(routerConfig)
  be4Each(router)
  afterEach(router)
  return router
}

export default createVueRouter;

Logo

前往低代码交流专区

更多推荐