vuex 需要 全局掛載 Vue.prototype.$store 嗎?

 

官方:https://vuex.vuejs.org/zh/guide/

有說明:

爲了在 Vue 組件中訪問 this.$store property,你需要爲 Vue 實例提供創建好的 store。Vuex 提供了一個從根組件向所有子組件,以 store 選項的方式“注入”該 store 的機制:

所以已經注入,不需要用  Vue.prototype.$store = store 重複掛載

直接使用 this.$store

methods: {
  increment() {
    this.$store.commit('increment')
    console.log(this.$store.state.count)
  }
}

 

 

 

 

 

 

 

 

.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章