解決導航守衛使用不了this.$store

在vue router的導航守衛如beforeEach()中是無法直接通過this.$store去操作vuex的,因爲這裏的this指向不一致。

  • 解決方式是在router的index.js中引入初始化好的store
import store from '@/store'
  • 然後在導航守衛中可直接拿到router了
/**導航守衛 */
router.beforeEach((to, form, next) => {
  console.log(store.getters)
})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章