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)
  }
}

 

 

 

 

 

 

 

 

.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM