在Vuex商店中使用vue-router

时间:2017-07-13 20:45:44

标签: vue.js vuejs2 vue-router vuex

我想使用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})
    },

谢谢!

1 个答案:

答案 0 :(得分:0)

答案是我不得不在商店中使用正常的this.$router语法,而只是执行此操作:router.push({name:'summary'})。谢谢!