Vuex的使用及知識(shí)點(diǎn)筆記
1 入門(mén)
vuex是為vue.js專(zhuān)門(mén)提供的狀態(tài)管理模式
簡(jiǎn)單解釋?zhuān)簩⑺薪M件公用的數(shù)據(jù)、方法提取到指定的地方,進(jìn)行統(tǒng)一管理
2 安裝
下載vuex
npm i vuex --save
在src根目錄中新建一個(gè)store文件夾并新建一個(gè)index.js文件,并在index.js中引入vue和vuex
import Vue from 'vue' //導(dǎo)入vuex插件 import Vuex from 'vuex' //使用vuex插件 Vue.use(Vuex) export default new Vuex.Store({ state:{//state相當(dāng)于普通組件中的data數(shù)據(jù)域 }, getters:{//getter相當(dāng)于computed對(duì)象 }, mutations:{//mutations相當(dāng)于methods對(duì)象 }, actions:{ }, modules:{//分割模塊 } })
在main.js中導(dǎo)入index.js文件并掛載
3 核心概念的使用
3.1 state
state為vuex中的公共狀態(tài),我們可以看成state為所有組件的data,用于保存所有組件的公共數(shù)據(jù)·。
export default new Vuex.Store({ state:{//state相當(dāng)于普通組件中的data數(shù)據(jù)域 names:['胡桃','甘雨'] } })
在任意組件內(nèi)可以使用this.$store.state.names
來(lái)獲取到state里面的數(shù)據(jù)
<p>{{this.$store.state.names}}</p>
3.2 getters
vuex的getters屬性可以理解為所有組件的computed屬性,也就是計(jì)算屬性.getters的返回值會(huì)根據(jù)其依賴(lài)緩存起來(lái)的,只有當(dāng)依賴(lài)的值發(fā)生改變,getters才會(huì)重新計(jì)算
export default new Vuex.Store({ state:{//state相當(dāng)于普通組件中的data數(shù)據(jù)域 names:['胡桃','甘雨'], num:5 }, getters:{//getter相當(dāng)于computed對(duì)象 add(state){//state的數(shù)據(jù)會(huì)自動(dòng)傳入add的方法 return state.num+5 } } })
在任意的組件內(nèi)使用this.$store.getters.add
調(diào)用計(jì)算的方法
<p>{{this.$store.state.names}}</p> <p>{{this.$store.getters.add}}</p>
3.3 mutations
vuex中的mutations可以理解為所有組件的methods方法。
mutations對(duì)象中保存了更改數(shù)據(jù)的回調(diào)函數(shù)。
第一個(gè)參數(shù)為state,第二個(gè)參數(shù)為payload,相當(dāng)于自定義參數(shù)
export default new Vuex.Store({ state:{//state相當(dāng)于普通組件中的data數(shù)據(jù)域 names:['胡桃','甘雨'], num:5 }, getters:{//getter相當(dāng)于computed對(duì)象 add(state){ return state.num+5 } }, mutations:{//mutations相當(dāng)于methods對(duì)象 add(state,payload){//注意:必須傳入state參數(shù),payload為自定義參數(shù) state.num+=payload; } } })
在任意組件內(nèi)通過(guò)this.$store.commit()
觸發(fā)mutations內(nèi)的方法
<template> <div id='Home'> <p>{{this.$store.state.names}}</p> <p>{{this.$store.getters.add}}</p> <p><input type="text" v-model="this.$store.state.num"/> <span @click="add()">+</span></p> </div> </template> <script> export default{ name:'Home', methods:{ add(){ // this.$store.commit('evnetName',自定義的參數(shù)) this.$store.commit('add',5) } } } </script>
每一次點(diǎn)擊都會(huì)給state里面的num數(shù)據(jù)加5
3.4 actions
actions和mutations類(lèi)似,不同點(diǎn)在于:
1、actions提交的是mutations,而不是直接變更狀態(tài)
2、actions中包含異步,mutations中絕對(duì)不允許出現(xiàn)異步
3、actions中回調(diào)函數(shù)的第一個(gè)參數(shù)為context,得到一個(gè)與store具有相同屬性和方法的對(duì)象
export default new Vuex.Store({ state:{//state相當(dāng)于普通組件中的data數(shù)據(jù)域 names:['胡桃','甘雨'], num:5 }, getters:{//getter相當(dāng)于computed對(duì)象 add(state){ return state.num+5 } }, mutations:{//mutations相當(dāng)于methods對(duì)象 add(state,payload){//注意:必須傳入state參數(shù),payload為自定義參數(shù) state.num+=payload; } }, actions:{ addSync(context,payload){ setTimeout(()=>{ //add為mutations內(nèi)定義的函數(shù) //通過(guò)commit調(diào)用mutations內(nèi)的函數(shù) context.commit('add',payload) },1000) } }, })
組件內(nèi)綁定事件
<p><input type="text" v-model="this.$store.state.num"/> <span @click="addSync()">+</span></p>
通過(guò) this.$store.dispatch
調(diào)用actions內(nèi)的異步方法
methods:{ addSync(){ this.$store.dispatch('addSync',2) } }
測(cè)試的效果就是每次點(diǎn)擊之后,要過(guò)1s才會(huì)改變state里面的數(shù)值num
3.5 modules
由于使用單一狀態(tài)樹(shù),應(yīng)用的所有狀態(tài)會(huì)集中到一個(gè)比較大的對(duì)象。
當(dāng)應(yīng)用變得非常復(fù)雜時(shí),store 對(duì)象就有可能變得相當(dāng)臃腫。
為了解決以上問(wèn)題,Vuex 允許我們將 store 分割成模塊(module)。
每個(gè)模塊擁有自己的 state、mutation、action、getter、甚至是嵌套子模塊——從上至下進(jìn)行同樣方式的分割
const moduleA = { state: { ... }, mutations: { ... }, actions: { ... }, getters: { ... } } const moduleB = { state: { ... }, mutations: { ... }, actions: { ... } } const store = new Vuex.Store({ modules: { a: moduleA, b: moduleB } })
在組件內(nèi)分模塊進(jìn)行訪(fǎng)問(wèn)
<h2>{{this.$store.state.a.count}}</h2> <h2>{{this.$store.state.b.msg}}</h2>
4 輔助函數(shù)的使用
mapState、mapGetters、mapMutations、mapActions
引入
import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'
在computed中使用擴(kuò)展運(yùn)算符進(jìn)行定義
export default { name: 'list', data () { return { } }, computed:{ ...mapState({ //Index則代表state的num屬性 index:'num' }), ...mapGetters({ // 屬性值add則代表getters內(nèi)的add方法 add:'add' }) }, methods:{ ...mapMutation({ addNum:'addNum' }), ...mapActions({ }) } }
vuex好文檔推薦 參考資料:https://vuex.vuejs.org/zh/
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Element Plus 日期選擇器獲取選中的日期格式(當(dāng)前日期/時(shí)間戳格式)
如果想要獲取選中的日期時(shí)間就需要通過(guò),Element Plus 日期選擇器?format屬性和value-format屬性,format指定輸入框的格式,value-format?指定綁定值的格式,本篇文章就給大家介紹Element Plus 日期選擇器獲取選中的日期格式(當(dāng)前日期/時(shí)間戳格式),感興趣的朋友一起看看吧2023-10-10vue如何實(shí)現(xiàn)清空this.$route.query的值
這篇文章主要介紹了vue如何實(shí)現(xiàn)清空this.$route.query的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue中關(guān)于v-for循環(huán)key值問(wèn)題的研究
這篇文章主要介紹了vue中關(guān)于v-for循環(huán)key值問(wèn)題的研究,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06詳解mpvue scroll-view自動(dòng)回彈bug解決方案
設(shè)置了scroll-top的scroll-view組件,在組件所在vue實(shí)例data發(fā)生改變時(shí)會(huì)自動(dòng)回彈到最上方,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10Vue router動(dòng)態(tài)路由實(shí)現(xiàn)過(guò)程
Vue動(dòng)態(tài)路由(約定路由),聽(tīng)起來(lái)好像很玄乎的樣子,但是你要是理解了實(shí)現(xiàn)思路,你會(huì)發(fā)現(xiàn)沒(méi)有想象中的那么難,下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)動(dòng)態(tài)路由添加功能的簡(jiǎn)單方法,需要的朋友可以參考下2023-03-03vue el-date-picker 開(kāi)始日期不能大于結(jié)束日期的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue el-date-picker 開(kāi)始日期不能大于結(jié)束日期的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01