【问题标题】:Using vue-router in Vuex store在 Vuex 商店中使用 vue-router
【发布时间】:2017-12-18 19:46:57
【问题描述】:

我想使用 vue-router 从 Vuex 商店路由到新组件。是否可以从 Vuex 商店访问路由器?我已经看到了一堆关于它的线程,但没有什么对我真正有用...下面的错误是我尝试使用路由器时得到的。

[Vue 警告]:挂载钩子错误:“TypeError: WEBPACK_IMPORTED_MODULE_2_vue_router.a.push 不是函数”

这是我的商店:

SENTENCE_TRACKER({commit, state}){
  let chosenSentence
  if(state.currSent < state.res.length) {
    chosenSentence = state.res[state.currSent];
    console.log(chosenSentence);
    commit('CHINESE', {chineseData:chosenSentence.chinese})
    commit('ENGLISH', {englishData:chosenSentence.english})
    commit('PINYIN', {pinyinData:chosenSentence.pinyin})
    commit('AUDIO', {audioData:chosenSentence.audio})
    commit('WBW', {wbwData:chosenSentence.wbw})
    state.currSent++
  } else {
     // THIS IS WHERE I'D LIKE TO USE VUE-ROUTER
     router.push({path:'/summary'})
  }
     let arr = []
     state.wbw.map(function(i){
       arr.push('')
     })
     commit('WBW_STATE', {wbwStateData: arr})

     let shuffled = state.wbw.slice(), i, j, k;
     for (i = shuffled.length; i; i--) {
       j = Math.floor(Math.random() * i);
       k = shuffled[i - 1];
       shuffled[i - 1] = shuffled[j];
       shuffled[j] = k;
     }
     commit('SHUFFLED', {shuffledData: shuffled})
    },

谢谢!

【问题讨论】:

  • 你能显示设置router的代码吗?

标签: vue.js vuejs2 vue-router vuex


【解决方案1】:

答案是我不必在商店中使用普通的this.$router 语法,而是这样做:router.push({name:'summary'})。谢谢!

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2019-08-18
    • 2021-12-05
    • 2020-01-10
    • 1970-01-01
    • 2020-08-25
    • 2021-04-10
    • 2021-07-22
    • 2019-10-27
    相关资源
    最近更新 更多