什么是vuex?他有什么作用?如何改變store中的狀態(state)?


vuex是一個專為vue.js應用程序開發的狀態管理模式。vuex解決了組件之間同一狀態的共享問題。
        當我們的應用遇到多個組件之間的共享問題時會需要
        狀態管理核心狀態管理有5個核心,分別是state、getter、mutation、action以及module。
    1.state
        state為單一狀態樹,在state中需要定義我們所需要管理的數組、對象、字符串等等,只有在這里定義了,
        在vue.js的組件中才能獲取你定義的這個對象的狀態。
    2.簡單的 store 模式
    var store = { 
        debug: true, 
        state: { 
        message: 'Hello!' 
    }, 
    setMessageAction (newValue) {
         if (this.debug) 
         console.log('setMessageAction triggered with', newValue) 
         this.state.message = newValue 
     },
      clearMessageAction () { 
          if (this.debug) console.log('clearMessageAction triggered') this.state.message = '' 
      } 
     }
     
     所有 store 中 state 的改變,都放置在 store 自身的 action 中去管理。
     這種集中式狀態管理能夠被更容易地理解哪種類型的 mutation 將會發生,以及它們是如何被觸發。
     當錯誤出現時,我們現在也會有一個 log 記錄 bug 之前發生了什么。
     
     此外,每個實例/組件仍然可以擁有和管理自己的私有狀態:
     var vmA = new Vue({ 
         data: { 
         privateState: {}, 
         sharedState: store.state 
         } 
     }) 
     var vmB = new Vue({ 
         data: { 
         privateState: {}, 
         sharedState: store.state 
         } 
     })

 


免責聲明!

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



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