vuex狀態(tài)管理淺談之mapState用法
一、state
state是什么?
定義:state(vuex) ≈ data (vue)
vuex的state和vue的data有很多相似之處,都是用于存儲(chǔ)一些數(shù)據(jù),或者說(shuō)狀態(tài)值.這些值都將被掛載 數(shù)據(jù)和dom的雙向綁定事件,也就是當(dāng)你改變值的時(shí)候可以觸發(fā)dom的更新.
雖然state和data有很多相似之處,但state在使用的時(shí)候一般被掛載到子組件的computed計(jì)算屬性上,這樣有利于state的值發(fā)生改變的時(shí)候及時(shí)響應(yīng)給子組件.如果你用data去接收$store.state,當(dāng)然可以接收到值,但由于這只是一個(gè)簡(jiǎn)單的賦值操作,因此state中的狀態(tài)改變的時(shí)候不能被vue中的data監(jiān)聽(tīng)到,當(dāng)然你也可以通過(guò)watch $store去解決這個(gè)問(wèn)題,那你可以針是一個(gè)杠精
綜上所述,請(qǐng)用computed去接收state,如下
//state.js
let state = {
count: 1,
name: 'dkr',
sex: '男',
from: 'china'
}
export default state
<template>
<div id="example">
<button @click="decrement">-</button>
{{count}}
{{dataCount}}
<button @click="increment">+</button>
</div>
</template>
<script>
export default {
data () {
return {
dataCount: this.$store.state.count //用data接收
}
},
computed:{
count(){
return this.$store.state.count //用computed接收
}
}
methods: {
increment () {
this.$store.commit('increment')
},
decrement () {
this.$store.commit('decrement')
}
}
}
</script>

二、mapstate輔助函數(shù)
mapState是什么?
mapState是state的輔助函數(shù).這么說(shuō)可能很難理解
抽象形容:mapState是state的語(yǔ)法糖,這么說(shuō)可能你還想罵我,因?yàn)槟愀静涣私馐裁唇凶稣Z(yǔ)法糖,事實(shí)上我說(shuō)的語(yǔ)法糖有自己的定義,什么是語(yǔ)法糖?我對(duì)語(yǔ)法糖的理解就是,用之前覺(jué)得,我明明已經(jīng)對(duì)一種操作很熟練了,并且這種操作也不存在什么問(wèn)題,為什么要用所謂的"更好的操作",用了一段時(shí)間后,真香!
實(shí)際作用:當(dāng)一個(gè)組件需要獲取多個(gè)狀態(tài)時(shí)候,將這些狀態(tài)都聲明為計(jì)算屬性會(huì)有些重復(fù)和冗余。為了解決這個(gè)問(wèn)題,我們可以使用 mapState 輔助函數(shù)幫助我們生成計(jì)算屬性,讓你少按幾次鍵
在使用mapState之前,要導(dǎo)入這個(gè)輔助函數(shù)
import { mapState } from 'vuex'
store.js
// store.js
/**
vuex的核心管理對(duì)象模塊:store
*/
import Vue from 'vue';
import Vuex from 'vuex';
import vuexTest from './modules/vuexTest';
Vue.use(Vuex)
// 狀態(tài)對(duì)象
const state = { // 初始化狀態(tài) 這里放置的狀態(tài)可以被多個(gè)組件共享
count: 1,
count1: 1,
count2: 2,
count3: 3,
name: 'daming'
};
const mutations = {};
const actions = {};
const getters = {};
export default new Vuex.Store({
state, // 狀態(tài)
mutations, // 包含多個(gè)更新state函數(shù)的對(duì)象
actions, // 包含多個(gè)隊(duì)形事件回調(diào)函數(shù)的對(duì)象
getters, // 包含多個(gè)getter計(jì)算屬性函數(shù)的對(duì)象
modules: { // 模塊化
vuexTest
}
});
1、在界面或組件中不使用mapState獲取vuex中state的狀態(tài)
雖然將所有的狀態(tài)放入Vuex,會(huì)使?fàn)顟B(tài)變化更顯式和易調(diào)試,但也會(huì)使代碼變得冗長(zhǎng)和不直觀(guān)。如果有些狀態(tài)嚴(yán)格屬于單個(gè)組件,最好還是作為組件的局部狀態(tài),比如temp變量,hello, number作為組件的局部狀態(tài)。
<!-- test.vue -->
<template>
<div id="example">
{{count}}
{{name}}
{{helloName}}
{{addNumber}}
</div>
</template>
<script>
export default {
data() {
return {
hello: 'hello',
number: 1,
}
},
computed: {
// 由于 Vuex 的狀態(tài)存儲(chǔ)是響應(yīng)式的,所以可以使用計(jì)算屬性來(lái)獲得某個(gè)狀態(tài)
// 通過(guò)下面的計(jì)算屬性,就可以在當(dāng)前組件中訪(fǎng)問(wèn)到count,name,helloName,addNumber 在模板中我們通過(guò)大括號(hào)符號(hào)打印出來(lái),當(dāng)然這些可以在vue中使用,比如在watch中監(jiān)聽(tīng),在mounted中使用
// 下面的計(jì)算屬性涉及到了vuex管理的狀態(tài)
count() { // 這實(shí)際上是ES6中對(duì)象的簡(jiǎn)化寫(xiě)法 完整寫(xiě)法是 count: function { return this.$store.state.count }
return this.$store.state.count
},
name() { // 這實(shí)際上是ES6中對(duì)象的簡(jiǎn)化寫(xiě)法 完整寫(xiě)法是 name: function { return this.$store.state.count }
return this.$store.state.count
},
helloName: function (state) { // 為了能夠使用 `this` 獲取局部狀態(tài),必須使用常規(guī)函數(shù),不能使用箭頭函數(shù)
return this.hello + this.$store.state.name
},
addNumber: function (state) { // 為了能夠使用 `this` 獲取局部狀態(tài),必須使用常規(guī)函數(shù),不能使用箭頭函數(shù)
return this.number + this.$store.state.count
}
// 但有一個(gè)問(wèn)題
// 當(dāng)一個(gè)組件需要獲取多個(gè)狀態(tài)的時(shí)候,將這些狀態(tài)都聲明為計(jì)算屬性會(huì)有些重復(fù)和冗余。比如上面的name(),count(),helloName(),顯得重復(fù),代碼冗長(zhǎng)
// 為了解決這個(gè)問(wèn)題,我們可以使用 mapState 輔助函數(shù)幫助我們生成計(jì)算屬性,讓你少按幾次鍵:
},
watch: {
helloName(newVal,oldVal){
console.log(newVal);
console.log(oldVal);
}
},
mounted(){
console.log(this.helloName);
}
}
</script>
2、在組件、界面中使用mapState獲取vuex中state的數(shù)據(jù)
<!-- test.vue -->
<template>
<div id="example">
{{count}}
{{count1}}
{{repeatCount}}
{{count3}}
{{name}}
{{helloName}}
{{addNumber}}
</div>
</template>
<script>
export default {
data() {
return {
hello: 'hello',
number: 1,
count2: 2
}
},
computed: {
/**
* 數(shù)組形式
* 當(dāng)映射的計(jì)算屬性的名稱(chēng)與 state 的子節(jié)點(diǎn)名稱(chēng)相同時(shí),我們也可以給 mapState 傳一個(gè)字符串?dāng)?shù)組。
* */
...mapState(["count", "name"]),
/**
* 對(duì)象形式
*/
...mapState({
count, // 這種就是count:"count", 的簡(jiǎn)寫(xiě)
count1: "count1",
repeatCount: "count2", // 當(dāng)組件中與vuex中的字符已經(jīng)出現(xiàn)重復(fù)時(shí),使用 repeatCount 來(lái)映射 store.state.count2
count3: (state) => { // 映射 count3 為 store.state.conut3的值
return state.count3
},
helloName: function (state) { // 為了能夠使用 `this` 獲取局部狀態(tài),必須使用常規(guī)函數(shù),不能使用箭頭函數(shù)
return this.hello + state.name
},
addNumber: function (state) { // 為了能夠使用 `this` 獲取局部狀態(tài),必須使用常規(guī)函數(shù),不能使用箭頭函數(shù)
return this.number + state.count
}
})
},
watch: {
helloName(newVal, oldVal) {
console.log(newVal);
console.log(oldVal);
}
},
mounted() {
console.log(this.helloName);
}
}
</script>
3、modules的vuexTest模塊中state數(shù)據(jù)
/**
* vuexTest.js
* modules 中的數(shù)據(jù)
*/
export default {
namespaced: true,
state: {
moduleVal: 1,
moduleName: "戰(zhàn)戰(zhàn)兢兢"
},
getters: {
},
mutations: {
},
actions: {
}
}
4、在界面或組件中不使用mapState獲取模塊modules vuexTest中state的狀態(tài)
<!-- module test.vue -->
<template>
<div id="example">
{{moduleVal}}
{{moduleName}}
{{moduleNameOther}}
</div>
</template>
<script>
export default {
data() {
return {
hello: 'hello',
number: 1,
}
},
computed: {
moduleVal(){
return this.$store.state.vuexTest.moduleVal
},
moduleName(){
return this.$store.state.vuexTest.moduleVal
},
moduleNameOther(){
// 當(dāng)組件中與vuex中的字符已經(jīng)出現(xiàn)重復(fù)時(shí),使用 moduleNameOther 來(lái)映射 store.state.vuexTest.moduleName
return this.$store.state.vuexTest.moduleVal
},
},
watch: {
helloName(newVal, oldVal) {
console.log(newVal);
console.log(oldVal);
}
},
mounted() {
console.log(this.addNumber);
}
}
</script>
5、在界面或組件中使用mapState獲取模塊modules vuexTest中state的狀態(tài)
<!-- module test.vue -->
<template>
<div id="example">
{{moduleVal}}
{{moduleName}}
{{moduleNameOther}}
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
data() {
return {
hello: 'hello',
number: 1,
}
},
computed: {
/**
* 數(shù)組形式
* 當(dāng)映射的計(jì)算屬性的名稱(chēng)與 與模塊中vuexTest中state 的子節(jié)點(diǎn)名稱(chēng)相同時(shí),我們也可以給 mapState 傳一個(gè)字符串?dāng)?shù)組,
* */
...mapState("vuexTest", ["moduleVal", "moduleName"]),
// "vuexTest" 指向模塊vuexTest,"moduleVal"表示store.vuexTest.moduleVal
/**
* 對(duì)象形式
*/
// 第一種對(duì)象方式
...mapState({
moduleVal: "vuexTest/moduleVal",
moduleNameOther: "vuexTest/moduleName" // 表示 moduleNameOther 映射到vuexTest模塊中moduleName
}),
...mapState("vuexTest", {
moduleVal, // 這種就是moduleVal:"moduleVal", 的簡(jiǎn)寫(xiě)
moduleName: "moduleName",
moduleNameOther: "moduleName", // 當(dāng)組件中與vuex中的字符已經(jīng)出現(xiàn)重復(fù)時(shí),使用 moduleNameOther 來(lái)映射 store.state.vuexTest.moduleName
moduleVal: (state) => { // 映射 moduleVal 為 store.state.vuexTest.moduleVal的值
return state.moduleVal
},
helloName: function (state) { // 為了能夠使用 `this` 獲取局部狀態(tài),必須使用常規(guī)函數(shù),不能使用箭頭函數(shù)
return this.hello + state.moduleName
},
addNumber(state) { // 為了能夠使用 `this` 獲取局部狀態(tài),必須使用常規(guī)函數(shù),不能使用箭頭函數(shù)
return this.number + state.moduleVal
}
})
},
watch: {
helloName(newVal, oldVal) {
console.log(newVal);
console.log(oldVal);
}
},
mounted() {
console.log(this.addNumber);
}
}
</script>總結(jié)
到此這篇關(guān)于vuex狀態(tài)管理淺談之mapState用法的文章就介紹到這了,更多相關(guān)vuex mapState用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3中簡(jiǎn)單使用Mock.js方法實(shí)例分析
這篇文章主要介紹了Vue3中簡(jiǎn)單使用Mock.js方法,mock.js在模擬后端接口數(shù)據(jù)響應(yīng)與協(xié)調(diào)統(tǒng)一前后端開(kāi)發(fā)接口規(guī)范方面有著重要的應(yīng)用,需要的朋友可以參考下2023-04-04
Vue3 + TypeScript 開(kāi)發(fā)總結(jié)
本文直接上 Vue3 + TypeScript + Element Plus 開(kāi)發(fā)的內(nèi)容,感興趣的話(huà)一起來(lái)看看吧2021-08-08
vue使用keep-alive無(wú)效以及include和exclude用法解讀
這篇文章主要介紹了vue使用keep-alive無(wú)效以及include和exclude用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue文件上傳讀取文件數(shù)據(jù)進(jìn)行格式校驗(yàn)方式
該文章描述了使用Element-UI的`el-upload`組件實(shí)現(xiàn)文件上傳前的內(nèi)容校驗(yàn),通過(guò)`beforeUpload`鉤子讀取并解析文件內(nèi)容,校驗(yàn)其格式和數(shù)據(jù)有效性,確保符合要求后才進(jìn)行上傳,詳細(xì)介紹了校驗(yàn)邏輯及上傳流程2026-05-05
vue監(jiān)聽(tīng)瀏覽器原生返回按鈕,進(jìn)行路由轉(zhuǎn)跳操作
這篇文章主要介紹了vue監(jiān)聽(tīng)瀏覽器原生返回按鈕,進(jìn)行路由轉(zhuǎn)跳操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
vue2+tracking實(shí)現(xiàn)PC端的人臉識(shí)別示例
本文主要介紹了vue2+tracking實(shí)現(xiàn)PC端的人臉識(shí)別示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
在 Vue 3 中設(shè)置 `@` 指向根目錄的幾種常見(jiàn)方法匯總
在 Vue 3 項(xiàng)目開(kāi)發(fā)中,為了方便管理和引用文件路徑,設(shè)置 @ 指向根目錄是一項(xiàng)常見(jiàn)的需求,下面給大家分享在Vue3中設(shè)置 `@` 指向根目錄的方法匯總,感興趣的朋友一起看看吧2024-06-06

