關(guān)于vuex的原理及使用方法
簡介
Vuex 是 Vue.js 應(yīng)用的狀態(tài)管理模式,它為應(yīng)用內(nèi)的所有組件提供集中式的狀態(tài)(數(shù)據(jù))管理。
可以幫我們管理 Vue 通用的數(shù)據(jù) (多組件共享的數(shù)據(jù))。
Vuex的構(gòu)成
state
:state 是 Vuex 的數(shù)據(jù)中心,也就是說state是用來存儲(chǔ)數(shù)據(jù)的。getters
:state 對象讀取方法。Vue Components 通過該方法讀取全局 state 對象。mutations
:狀態(tài)改變操作方法。是 Vuex 修改 state 的唯一推薦方法,其他修改方式在嚴(yán)格模式下將會(huì)報(bào)錯(cuò)。 該方法只能進(jìn)行同步操作, 且方法名只能全局唯一。 操作之中會(huì)有一些 hook 暴露出來, 以進(jìn)行state 的監(jiān)控等。actions
:操作行為處理模塊。 負(fù)責(zé)處理 Vue Components 接收到的所有交互行為。 包含同步/異步操作, 支持多個(gè)同名方法, 按照注冊的順序依次觸發(fā)。 向后臺(tái) API 請求的操作就在這個(gè)模塊中進(jìn)行, 包括觸發(fā)其他 action 以及提交 mutation 的操作。 該模塊提供了 Promise的封裝, 以支持 action 的鏈?zhǔn)接|發(fā)。modules
:將 Store 分割成模塊,每個(gè)模塊擁有自己的 State、Getters、Mutations Actions。
Vuex的使用
1、安裝 Vuex:npm install vuex
。
2、創(chuàng)建store示例
store對象
import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); export default new Vuex.Store({ state: { count: 0 }, mutations: { increment(state) { state.count++; } }, actions: { increment({ commit }) { commit('increment'); } }, getters: { count: state => state.count } });
在 Vue 根實(shí)例中注冊store
import Vue from 'vue'; import App from './App.vue'; import store from './store'; new Vue({ store, render: h => h(App) }).$mount('#app');
在組件中使用 Store
export default { computed: { count() { return this.$store.state.count; } }, methods: { increment() { this.$store.dispatch('increment'); } } };
使用Vuex內(nèi)容擴(kuò)展
在真正開發(fā)中使用vuex時(shí)會(huì)有好多細(xì)節(jié)知識(shí)和注意事項(xiàng),下面我們擴(kuò)展一下,僅供參看
Vue 組件中獲得 Vuex 狀態(tài)(State)
方式一: this.$store.state獲取
通過在根實(shí)例中注冊 store 選項(xiàng),該 store 實(shí)例會(huì)注入到根組件下的所有子組件中,且子組件能通過 this.$store 訪問到
computed: { count () { return this.$store.state.count } }
方式二: mapState 輔助函數(shù)獲?。ㄍ扑])
當(dāng)一個(gè)組件需要獲取多個(gè)狀態(tài)時(shí)候,將這些狀態(tài)都聲明為計(jì)算屬性會(huì)有些重復(fù)和冗余。為了解決這個(gè)問題,我們可以使用 mapState 輔助函數(shù)幫助我們生成計(jì)算屬性
<template> <div>{{count}}</div> </template> <script> import { mapState }from 'vuex export default{ computed:{ ...mapstate(['count']) } } </script>
Getter的定義和獲取方式
定義getters:
需要顯示所有大于5的數(shù)據(jù),正常的方式,是需要list在組件中進(jìn)行再一步的處理,但是getters可以幫助我們實(shí)現(xiàn)它
【下面getters引用的state中的數(shù)據(jù):list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]】
getters: { // getters函數(shù)的第一個(gè)參數(shù)是 state // 必須要有返回值 filterList: state => state.list.filter(item => item > 5) }
獲取getters:
方式一: 通過屬性訪問
this.$store.getters.filterList
方式二:輔助函數(shù) - mapGetters
<template> <div>{{filterList}}</div> </template> <script> import { mapGetters }from 'vuex export default{ computed:{ ...mapGetters(['filterList']) } } </script>
Vue組件中調(diào)用Vuex:mutations中的方法
- 直接通過 store 調(diào)用 $store.commit('模塊名/xxx ', 額外參數(shù))
- 通過 mapMutations 映射
1、默認(rèn)根級(jí)別的映射 mapMutations([ ‘xxx’ ])
2、子模塊的映射 mapMutations(‘模塊名’, [‘xxx’]) - 需要開啟命名空間
方式一:普通調(diào)用方式
- this.$store.commit('addCount') 此為不帶參數(shù)的寫法
- this.$store.commit('addCount', 10) 此為帶參數(shù)寫法
<template> <div @click="addData">{{count}}</div> </template> <script> export default{ methods: { addData() { this.$store.commit('increment') } } } </script>
方式二:輔助函數(shù)- mapMutations
mapMutations是將所有mutations里面的方法映射為實(shí)例methods里面的方法
<template> <div @click="addData">{{count}}</div> <div @click="increment">{{count}}</div> </template> <script> export default{ import { mapMutations } from 'vuex' methods: { // 有別名的寫法[對應(yīng)第一行div] ...mapMutations({ addData:'increment' }) // 同名的簡寫[對應(yīng)第二行div] ...mapMutations(['increment']) } } </script>
Vue組件獲取Vuex:actions中的方法
- 直接通過 store 調(diào)用 $store.dispatch('模塊名/xxx ', 額外參數(shù))
- 通過 mapActions 映射
1、默認(rèn)根級(jí)別的映射 mapActions([ ‘xxx’ ])
2、子模塊的映射 mapActions(‘模塊名’, [‘xxx’]) - 需要開啟命名空間
方式一:普通調(diào)用方式
this.$store.dispatch('increment') this.$store.dispatch('increment',{num: 10})
<template> <div @click="addData">{{count}}</div> </template> <script> export default{ methods: { addData() { this.$store.dispatch('increment') } } } </script>
方式二:輔助函數(shù) -mapActions
mapActions 是把位于 actions中的方法提取了出來,映射到組件methods中
<template> <div @click="increment">{{count}}</div> </template> <script> export default{ import { mapActions } from 'vuex' methods: { ...mapActions (['increment']) } } </script>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js 通過jQuery ajax獲取數(shù)據(jù)實(shí)現(xiàn)更新后重新渲染頁面的方法
今天小編小編就為大家分享一篇Vue.js 通過jQuery ajax獲取數(shù)據(jù)實(shí)現(xiàn)更新后重新渲染頁面的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Vue實(shí)現(xiàn)簡單可擴(kuò)展甘特圖的方法詳解
Ganttastic是一個(gè)小型的Vue.js組件,用于在Web應(yīng)用程序上呈現(xiàn)一個(gè)可配置的、可拖動(dòng)的甘特圖。本文就將用它來實(shí)現(xiàn)簡單可擴(kuò)展的甘特圖,感興趣的可以嘗試一下2022-11-11el-tree設(shè)置選中高亮/焦點(diǎn)高亮、選中節(jié)點(diǎn)加深背景及更改字體顏色等的方法
el-tree默認(rèn)有較淺的背景色,這里業(yè)務(wù)需要,選中節(jié)點(diǎn)的字體高亮,更改顏色,下面這篇文章主要給大家介紹了關(guān)于el-tree選中高亮/焦點(diǎn)高亮、選中節(jié)點(diǎn)加深背景及更改字體顏色等的設(shè)置方法,需要的朋友可以參考下2022-12-12vue如何封裝自己的Svg圖標(biāo)組件庫(svg-sprite-loader)
這篇文章主要介紹了vue如何封裝自己的Svg圖標(biāo)組件庫(svg-sprite-loader),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04在Vue中實(shí)現(xiàn)二維碼生成與掃描功能的方法
二維碼是一種廣泛應(yīng)用于各種場合的編碼方式,它可以將信息編碼成一張二維圖案,方便快捷地傳遞信息,在Vue.js中,我們可以使用一些庫和組件來實(shí)現(xiàn)二維碼的生成和掃描,本文將介紹如何在Vue中實(shí)現(xiàn)二維碼的生成和掃描的方法2023-06-06從零開始實(shí)現(xiàn)Vue簡單的Toast插件
這篇文章主要給大家介紹了如何從零開始實(shí)現(xiàn)Vue簡單的Toast插件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12Vue3之Mixin的使用方式(全局,局部,setup內(nèi)部使用)
這篇文章主要介紹了Vue3之Mixin的使用方式(全局,局部,setup內(nèi)部使用),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10詳解vue2如何實(shí)現(xiàn)點(diǎn)擊預(yù)覽本地文件
這篇文章主要為大家詳細(xì)介紹了vue2如何實(shí)現(xiàn)點(diǎn)擊預(yù)覽本地的word、excle、pdf文件,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04