Vuex的使用及知識點筆記
1 入門
vuex是為vue.js專門提供的狀態(tài)管理模式
簡單解釋:將所有組件公用的數據、方法提取到指定的地方,進行統(tǒng)一管理
2 安裝
下載vuex
npm i vuex --save

在src根目錄中新建一個store文件夾并新建一個index.js文件,并在index.js中引入vue和vuex
import Vue from 'vue'
//導入vuex插件
import Vuex from 'vuex'
//使用vuex插件
Vue.use(Vuex)
export default new Vuex.Store({
state:{//state相當于普通組件中的data數據域
},
getters:{//getter相當于computed對象
},
mutations:{//mutations相當于methods對象
},
actions:{
},
modules:{//分割模塊
}
})在main.js中導入index.js文件并掛載

3 核心概念的使用
3.1 state
state為vuex中的公共狀態(tài),我們可以看成state為所有組件的data,用于保存所有組件的公共數據·。
export default new Vuex.Store({
state:{//state相當于普通組件中的data數據域
names:['胡桃','甘雨']
}
})在任意組件內可以使用this.$store.state.names來獲取到state里面的數據
<p>{{this.$store.state.names}}</p>
3.2 getters
vuex的getters屬性可以理解為所有組件的computed屬性,也就是計算屬性.getters的返回值會根據其依賴緩存起來的,只有當依賴的值發(fā)生改變,getters才會重新計算
export default new Vuex.Store({
state:{//state相當于普通組件中的data數據域
names:['胡桃','甘雨'],
num:5
},
getters:{//getter相當于computed對象
add(state){//state的數據會自動傳入add的方法
return state.num+5
}
}
})
在任意的組件內使用this.$store.getters.add調用計算的方法
<p>{{this.$store.state.names}}</p>
<p>{{this.$store.getters.add}}</p>3.3 mutations
vuex中的mutations可以理解為所有組件的methods方法。
mutations對象中保存了更改數據的回調函數。
第一個參數為state,第二個參數為payload,相當于自定義參數
export default new Vuex.Store({
state:{//state相當于普通組件中的data數據域
names:['胡桃','甘雨'],
num:5
},
getters:{//getter相當于computed對象
add(state){
return state.num+5
}
},
mutations:{//mutations相當于methods對象
add(state,payload){//注意:必須傳入state參數,payload為自定義參數
state.num+=payload;
}
}
})
在任意組件內通過this.$store.commit()觸發(fā)mutations內的方法
<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',自定義的參數)
this.$store.commit('add',5)
}
}
}
</script>每一次點擊都會給state里面的num數據加5
3.4 actions
actions和mutations類似,不同點在于:
1、actions提交的是mutations,而不是直接變更狀態(tài)
2、actions中包含異步,mutations中絕對不允許出現異步
3、actions中回調函數的第一個參數為context,得到一個與store具有相同屬性和方法的對象
export default new Vuex.Store({
state:{//state相當于普通組件中的data數據域
names:['胡桃','甘雨'],
num:5
},
getters:{//getter相當于computed對象
add(state){
return state.num+5
}
},
mutations:{//mutations相當于methods對象
add(state,payload){//注意:必須傳入state參數,payload為自定義參數
state.num+=payload;
}
},
actions:{
addSync(context,payload){
setTimeout(()=>{
//add為mutations內定義的函數
//通過commit調用mutations內的函數
context.commit('add',payload)
},1000)
}
},
})
組件內綁定事件
<p><input type="text" v-model="this.$store.state.num"/> <span @click="addSync()">+</span></p>
通過 this.$store.dispatch調用actions內的異步方法
methods:{
addSync(){
this.$store.dispatch('addSync',2)
}
}
測試的效果就是每次點擊之后,要過1s才會改變state里面的數值num
3.5 modules
由于使用單一狀態(tài)樹,應用的所有狀態(tài)會集中到一個比較大的對象。
當應用變得非常復雜時,store 對象就有可能變得相當臃腫。
為了解決以上問題,Vuex 允許我們將 store 分割成模塊(module)。
每個模塊擁有自己的 state、mutation、action、getter、甚至是嵌套子模塊——從上至下進行同樣方式的分割
const moduleA = {
state: { ... },
mutations: { ... },
actions: { ... },
getters: { ... }
}
const moduleB = {
state: { ... },
mutations: { ... },
actions: { ... }
}
const store = new Vuex.Store({
modules: {
a: moduleA,
b: moduleB
}
})
在組件內分模塊進行訪問
<h2>{{this.$store.state.a.count}}</h2>
<h2>{{this.$store.state.b.msg}}</h2>
4 輔助函數的使用
mapState、mapGetters、mapMutations、mapActions
引入
import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'在computed中使用擴展運算符進行定義
export default {
name: 'list',
data () {
return {
}
},
computed:{
...mapState({
//Index則代表state的num屬性
index:'num'
}),
...mapGetters({
// 屬性值add則代表getters內的add方法
add:'add'
})
},
methods:{
...mapMutation({
addNum:'addNum'
}),
...mapActions({
})
}
}

vuex好文檔推薦 參考資料:https://vuex.vuejs.org/zh/
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Element Plus 日期選擇器獲取選中的日期格式(當前日期/時間戳格式)
如果想要獲取選中的日期時間就需要通過,Element Plus 日期選擇器?format屬性和value-format屬性,format指定輸入框的格式,value-format?指定綁定值的格式,本篇文章就給大家介紹Element Plus 日期選擇器獲取選中的日期格式(當前日期/時間戳格式),感興趣的朋友一起看看吧2023-10-10
詳解mpvue scroll-view自動回彈bug解決方案
設置了scroll-top的scroll-view組件,在組件所在vue實例data發(fā)生改變時會自動回彈到最上方,非常具有實用價值,需要的朋友可以參考下2018-10-10
vue el-date-picker 開始日期不能大于結束日期的實現代碼
這篇文章主要介紹了vue el-date-picker 開始日期不能大于結束日期的實現代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01

