13. vueX 的使用方法

vueX 的使用方法(狀態(tài)管理器)

<br />

1. state 的用法及特點(diǎn) state是共享狀態(tài)

image

image

<br />

2. mutations 的用法及特點(diǎn) 只有 mutations 才有權(quán)利修改 state 的值

image

image

image

<br />

3. Action的用法及特點(diǎn) Action 執(zhí)行異步操作 但不能修改 state 的值(要修改 state 的值,必須通過 Mutations 來修改)

image

image

image

<br />

4. getter的用法及特點(diǎn) getter 是用來裝飾 state 的數(shù)據(jù) 相當(dāng)于 vue 中的計(jì)算屬性

image

image

<br />

重點(diǎn): this 只需要寫在js文件里面,其余文件不用寫this

<br />

遇到 vue 頁面有紅色的報(bào)錯(cuò)怎么解決

image

1. 先在vue項(xiàng)目中安裝vueX 執(zhí)行 npm i vuex --save

// store 文件夾下 index.js 就是 vuex 的文件
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
// state 提供公共數(shù)據(jù)源
  state: {
    count: 0, // 第一種方式訪問   this.$store.state.count
  },
  mutations: {
  },
  actions: {
  },
  getter: {
  },
  modules: {
  }
})


// 在 main.js 中 導(dǎo)入 store 并且注冊(cè)到 vue 實(shí)例中 
import store from './store'

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容