Vue中狀態(tài)管理器(vuex)詳解以及實際應(yīng)用場景
- 傳送門:Vue中 子組件向父組件傳值 及 .sync 修飾符 詳解
- 傳送門:Vue中 $ attrs、$ listeners 詳解及使用
- 傳送門:Vue中 事件總線(eventBus)詳解及使用
- 傳送門:Vue中 provide、inject 詳解及使用
Vue中 常見的組件通信方式可分為三類
父子通信
父向子傳遞數(shù)據(jù)是通過 props,子向父是通過 events($emit);
通過父鏈 / 子鏈也可以通信($parent / $children);
ref 也可以訪問組件實例;
provide / inject;
$attrs/$listeners;
兄弟通信
Bus;
Vuex;
跨級通信
Bus;
Vuex;
provide / inject、
$attrs / $listeners、
Vuex簡介
當(dāng)我們的應(yīng)用遇到多個組件共享狀態(tài)時,單向數(shù)據(jù)流的簡潔性很容易被破壞:
問題一:多個視圖依賴于同一狀態(tài)。
問題二:來自不同視圖的行為需要變更同一狀態(tài)。
對于問題一,傳參的方法對于多層嵌套的組件將會非常繁瑣,并且對于兄弟組件間的狀態(tài)傳遞無能為力。
對于問題二,我們經(jīng)常會采用父子組件直接引用或者通過事件來變更和同步狀態(tài)的多份拷貝。以上的這些模式非常脆弱,通常會導(dǎo)致無法維護的代碼。
因此,我們?yōu)槭裁床话呀M件的共享狀態(tài)抽取出來,以一個全局單例模式管理呢?
在這種模式下,我們的組件樹構(gòu)成了一個巨大的“視圖”,不管在樹的哪個位置,任何組件都能獲取狀態(tài)或者觸發(fā)行為!
通過定義和隔離狀態(tài)管理中的各種概念并通過強制規(guī)則維持視圖和狀態(tài)間的獨立性,我們的代碼將會變得更結(jié)構(gòu)化且易維護。
這就是 Vuex 背后的基本思想,借鑒了 Flux、Redux 和 The Elm Architecture。與其他模式不同的是,Vuex 是專門為 Vue.js 設(shè)計的狀態(tài)管理庫,以利用 Vue.js 的細粒度數(shù)據(jù)響應(yīng)機制來進行高效的狀態(tài)更新。
1. State
vuex中的數(shù)據(jù)源,我們需要保存的數(shù)據(jù)就保存在這里,在頁面通過 this.$store.state來獲取我們定義的數(shù)據(jù);
store\index.js
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { count:10 } })
views\about.vue
<template> <div class="about"> <h1>state:{{this.$store.state.count}}</h1> </div> </template>
效果:
2. Getters
Getter 相當(dāng)于 vue 中的 computed 計算屬性,getter 的返回值會根據(jù)它的依賴被緩存起來,且只有當(dāng)它的依賴值發(fā)生了改變才會被重新計算;
我們可以通過定義 vuex 的 Getter 來獲取,Getters 可以用于監(jiān)聽、state中的值的變化,返回計算后的結(jié)果;
這里我們修改 views\about.vue 文件如下:
<template> <div class="about"> <h1>getters:{{this.$store.getters.changeCount}}</h1> <h1>state:{{this.$store.state.count}}</h1> </div> </template>
再修改 store\index.js 文件如下,其中g(shù)etters中的getStateCount方法接收一個參數(shù)state,這個參數(shù)就是我們用來保存數(shù)據(jù)的那個對象;
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { count:10 }, getters:{ changeCount(state){ return state.count + 1 } } })
效果:
3. Mutations
如果需要修改 store 中的值唯一的方法就是提交 mutation 來修改;
我們現(xiàn)在 views\about.vue 文件中添加兩個按鈕,一個加1,一個減1;
這里我們點擊按鈕調(diào)用 addCount 和 reduceCount,然后在里面直接提交 mutations 中的方法修改值;
<template> <div class="about"> <h1>getters:{{this.$store.getters.changeCount}}</h1> <h1>state:{{this.$store.state.count}}</h1> <button @click="addCount"> + </button> <button @click="reduceCount"> - </button> </div> </template> <script> export default { data(){ return { n:10 } }, methods:{ addCount(){ this.$store.commit('add',this.n) // 傳遞參數(shù) }, reduceCount(){ this.$store.commit('reduce') } } } </script>
修改 store\index.js 文件,添加 mutations,在 mutations 中定義兩個函數(shù),用來對count加1和減1,這里定義的兩個方法就是上面commit提交的兩個方法如下:
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { count:10 }, getters:{ changeCount(state){ return state.count + 1 } }, mutations: { add(state,n){ state.count = state.count + n }, reduce(state){ state.count = state.count - 1 } } })
效果:
4. Actions
通過 mutations ,我們達到了修改store中狀態(tài)值的目的;
但是,mutation只能是同步,action 可以包含任意異步操作,在actions中提交mutation再去修改狀態(tài)值;
接下來我們修改 store\index.js文件:
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { count:10 }, getters:{ changeCount:state => state.count + 1 }, mutations: { add(state,n){ state.count = state.count + n }, reduce(state){ state.count = state.count - 1 } }, actions: { addFun(context,n){ context.commit('add',n) // 傳遞參數(shù) }, reduceFun(context){ context.commit('reduce') } } })
然后我們?nèi)バ薷膙iews\about.vue文件:
<template> <div class="about"> <h1>getters:{{this.$store.getters.changeCount}}</h1> <h1>state:{{this.$store.state.count}}</h1> <button @click="addCount"> + </button> <button @click="reduceCount"> - </button> </div> </template> <script> export default { data(){ return { n:10 } }, methods:{ addCount(){ this.$store.dispatch('addFun',this.n) // 傳遞參數(shù) }, reduceCount(){ this.$store.dispatch('reduceFun') } } } </script>
效果:
5. 使用 mapState、mapGetters、mapActions 簡化
如果我們不喜歡這種在頁面上使用 “this. s t r o e . s t a t e . c o u n t ” 和 “ t h i s . stroe.state.count” 和 “this. stroe.state.count”和“this.store.dispatch(‘funName’)” 這種很長的寫法,
那么我們可以使用 mapState、mapGetters、mapActions 就不會這么麻煩了;
我們修改 views\about.vue 文件如下:
<template> <div class="about"> <h1>getters:{{this.$store.getters.changeCount}}</h1> <h1>通過mapGetters獲取:{{changeCount}}</h1> <h1>通過mapState獲取:{{count}}</h1> <h1>state:{{this.$store.state.count}}</h1> <button @click="addCount"> + </button> <button @click="reduceCount"> - </button> </div> </template> <script> import {mapGetters,mapActions,mapState} from 'vuex' export default { data(){ return { n:10 } }, computed:{ ...mapState({ count:state => state.count }), ...mapGetters([ 'changeCount' ]) }, methods:{ ...mapActions( {reduceCount:'reduceFun'}, ), addCount(){ this.$store.dispatch('addFun',this.n) }, // reduceCount(){ // this.$store.dispatch('reduceFun') // } } } </script>
效果:
總結(jié)
到此這篇關(guān)于Vue中狀態(tài)管理器(vuex)詳解以及實際應(yīng)用場景的文章就介紹到這了,更多相關(guān)Vue狀態(tài)管理器vuex詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Vue如何實現(xiàn)顏色選擇與調(diào)色板功能
顏色選擇和調(diào)色板是Web開發(fā)中常用的功能,Vue作為一個流行的JavaScript框架,可以方便地實現(xiàn)顏色選擇和調(diào)色板功能,本文講講如何在Vue中進行顏色選擇和調(diào)色板吧2023-06-06vue 實現(xiàn)通過vuex 存儲值 在不同界面使用
今天小編就為大家分享一篇vue 實現(xiàn)通過vuex 存儲值 在不同界面使用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue3 Pinia獲取全局狀態(tài)變量的實現(xiàn)方式
這篇文章主要介紹了Vue3 Pinia獲取全局狀態(tài)變量的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05vue中的任務(wù)隊列和異步更新策略(任務(wù)隊列,微任務(wù),宏任務(wù))
這篇文章主要介紹了vue中的任務(wù)隊列和異步更新策略(任務(wù)隊列,微任務(wù),宏任務(wù)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08vue開發(fā)設(shè)計分支切換與cleanup實例詳解
這篇文章主要為大家介紹了vue開發(fā)設(shè)計分支切換與cleanup實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11