Vuex
-
概念:在Vue中實作集中式狀態(資料)管理的一個Vue插件,對vue應用中多個組件的共享狀態進行集中式的管理(讀/寫),也是一種組件間通信的方式,且適用于任意組件間的通信
-
使用場景:多個組件需要共享資料
-
搭建Vuex環境
- 創建檔案:src/store/index.js
// 該檔案用于創建Vuex中最為核心的store // 引入Vuex import Vue from 'vue' // 引入Vuex插件 import Vuex from "vuex" Vue.use(Vuex) // 準備actions--用于回應組件的動作 const actions = {} // 準備mutations--用于操作資料(state) const mutations = {} // 準備state--用于存盤資料 const state = {} const store = new Vuex.Store({ actions, mutations, state, }) export default store; - 在main.js中創建vm時傳入store配置項
// 引入創建的store import store from "./store" new Vue({ el: '#app', render: h=>h(App), // 使用store store, ... })
- 創建檔案:src/store/index.js
-
具體使用
- 組件使用
methods: { add() { this.$store.dispatch("add", params); }, ... } - src/store/index.js配置
// 準備actions--用于回應組件的動作 const actions = { add(context, value) { context.commiit("ADD", value); }, ... } // 準備mutations--用于操作資料(state) const mutations = { ADD(state, value) { state.sum += value; }, ... } // 準備state--用于存盤資料 const state = { sum: 0, ... }
- 組件使用
getters的使用
-
概念:當state中的資料需要經過加工后在使用時,可以使用getters加工
-
在store/index.js中追加getters配置
... // 準備getters--用于將state中的資料進行加工 const getters = { change(state){ return state.sum*10 }, } const store = new Vuex.Store({ ... getters, }) -
組件中讀取資料
$store.getters.change
組件中四個map方法的使用
- mapState方法:用于幫助我們映射state中的資料為計算屬性
computed: { // 通過mapState生成以上計算屬性從state中讀取資料(物件方式) ...mapState({sum2:'sum'}), // 通過mapState生成以上計算屬性從state中讀取資料(陣列方式),要求計算屬性名稱和state中讀取的屬性名一致 ...mapState(['sum']), } - mapGetters方法:用于幫助我們映射getters中的資料為自定義方法
methods: { // 通過mapGetters生成以上自定義方法從getters中讀取資料(物件方式) ...mapGetters({change:'change'}), // 通過mapGetters生成以上自定義方法從getters中讀取資料(陣列方式),要求自定義方法和getters中讀取的方法名稱一致 ...mapGetters([change]), } - mapActions方法:用于幫助我們生成與actions對話的方法,即:包含$store.dispatch(xxx)的函式
methods: { // 通過maActions生成incrementOdd,incrementWait(物件方式):如果需要傳遞引數,則在點擊呼叫時傳遞引數即可采用以下方式 ...mapActions({incrementOdd: 'addOdd', incrementWait: 'addWait'}), // 通過mapActions生成incrementOdd,incrementWait(陣列方式):如果需要傳遞引數,則在點擊呼叫時傳遞引數即可采用以下方式, // 要求計算屬性名稱和actions方法名一致 // ...mapActions(['addOdd', 'addWait']), } - mapMutations方法:用于幫助我們生成與mutations對話的方法,即:包含$store.commit(xxx)的函式
methods: { // 通過mapMutations生成increment,decrement(物件方式):如果需要傳遞引數,則在點擊呼叫時傳遞引數即可采用以下方式 ...mapMutations({increment: 'ADD', decrement: 'SUB'}), // 通過mapMutations生成increment,decrement(陣列方式):如果需要傳遞引數,則在點擊呼叫時傳遞引數即可采用以下方式, // 要求計算屬性名稱和mutations方法名一致 // ...mapMutations(['ADD', 'SUB']), }
模塊化+命名空間
-
目的:讓代碼更好的維護,讓多種資料分類更加明確,
-
修改store.js
const CountOptions { // 命名空間為true,組件即可使用該配置 namespaced: true, actions: {...}, mutations: {...}, state: {...}, getters: {...}, } const PersonOptions { // 命名空間為true,組件即可使用該配置 namespaced: true, actions: {...}, mutations: {...}, state: {...}, getters: {...}, } const store = new Vuex.Store({ modules: { // 模塊化編程,引入模塊配置(組件配置) CountAbout: CountOptions, PersonAbout: PersonOptions, } }) -
開啟命名空間后,組件中讀取state資料:
// 方式一:自己直接讀取 this.$store.state.PersonAbout.xxx; // 方式二:借助mapState讀取 ...mapState('CountAbout',{sum:'sum', sum2:'sum'}); -
開啟命名空間后,組件中讀取getters資料:
// 方式一:自己直接讀取 this.$store.getters['PersonAbout/xxx']; // 方式二:借助mapGetters讀取 ...mapGetters('CountAbout',{xxx:'xxx'}); -
開啟命名空間后,組件中呼叫dispatch:
// 方式一:自己直接呼叫dispatch this.$store.dispatch('CountAbout/xxx', params); // 方式二:借助mapActions讀取 ...mapActions('CountAbout',{xxx:'xxx'}); -
開啟命名空間后,組件中呼叫commit:
// 方式一:自己直接呼叫commit this.$store.commit('CountAbout/xxx', params); // 方式二:借助mapMutations讀取 ...mapMutations('CountAbout',{xxx:'xxx'});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/434439.html
標籤:其他
上一篇:requestAnimationFrame實作單張圖片無縫持續滾動
下一篇:ES6入門
