vuex狀態(tài)管理淺談之mapState用法
一、state
state是什么?
定義:state(vuex) ≈ data (vue)
vuex的state和vue的data有很多相似之處,都是用于存儲一些數(shù)據(jù),或者說狀態(tài)值.這些值都將被掛載 數(shù)據(jù)和dom的雙向綁定事件,也就是當你改變值的時候可以觸發(fā)dom的更新.
雖然state和data有很多相似之處,但state在使用的時候一般被掛載到子組件的computed計算屬性上,這樣有利于state的值發(fā)生改變的時候及時響應給子組件.如果你用data去接收$store.state,當然可以接收到值,但由于這只是一個簡單的賦值操作,因此state中的狀態(tài)改變的時候不能被vue中的data監(jiān)聽到,當然你也可以通過watch $store去解決這個問題,那你可以針是一個杠精
綜上所述,請用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ù).這么說可能很難理解
抽象形容:mapState是state的語法糖,這么說可能你還想罵我,因為你根本不了解什么叫做語法糖,事實上我說的語法糖有自己的定義,什么是語法糖?我對語法糖的理解就是,用之前覺得,我明明已經(jīng)對一種操作很熟練了,并且這種操作也不存在什么問題,為什么要用所謂的"更好的操作",用了一段時間后,真香!
實際作用:當一個組件需要獲取多個狀態(tài)時候,將這些狀態(tài)都聲明為計算屬性會有些重復和冗余。為了解決這個問題,我們可以使用 mapState 輔助函數(shù)幫助我們生成計算屬性,讓你少按幾次鍵
在使用mapState之前,要導入這個輔助函數(shù)
import { mapState } from 'vuex'
store.js
// store.js
/**
vuex的核心管理對象模塊:store
*/
import Vue from 'vue';
import Vuex from 'vuex';
import vuexTest from './modules/vuexTest';
Vue.use(Vuex)
// 狀態(tài)對象
const state = { // 初始化狀態(tài) 這里放置的狀態(tài)可以被多個組件共享
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, // 包含多個更新state函數(shù)的對象
actions, // 包含多個隊形事件回調(diào)函數(shù)的對象
getters, // 包含多個getter計算屬性函數(shù)的對象
modules: { // 模塊化
vuexTest
}
});
1、在界面或組件中不使用mapState獲取vuex中state的狀態(tài)
雖然將所有的狀態(tài)放入Vuex,會使狀態(tài)變化更顯式和易調(diào)試,但也會使代碼變得冗長和不直觀。如果有些狀態(tài)嚴格屬于單個組件,最好還是作為組件的局部狀態(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)存儲是響應式的,所以可以使用計算屬性來獲得某個狀態(tài)
// 通過下面的計算屬性,就可以在當前組件中訪問到count,name,helloName,addNumber 在模板中我們通過大括號符號打印出來,當然這些可以在vue中使用,比如在watch中監(jiān)聽,在mounted中使用
// 下面的計算屬性涉及到了vuex管理的狀態(tài)
count() { // 這實際上是ES6中對象的簡化寫法 完整寫法是 count: function { return this.$store.state.count }
return this.$store.state.count
},
name() { // 這實際上是ES6中對象的簡化寫法 完整寫法是 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
}
// 但有一個問題
// 當一個組件需要獲取多個狀態(tài)的時候,將這些狀態(tài)都聲明為計算屬性會有些重復和冗余。比如上面的name(),count(),helloName(),顯得重復,代碼冗長
// 為了解決這個問題,我們可以使用 mapState 輔助函數(shù)幫助我們生成計算屬性,讓你少按幾次鍵:
},
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ù)組形式
* 當映射的計算屬性的名稱與 state 的子節(jié)點名稱相同時,我們也可以給 mapState 傳一個字符串數(shù)組。
* */
...mapState(["count", "name"]),
/**
* 對象形式
*/
...mapState({
count, // 這種就是count:"count", 的簡寫
count1: "count1",
repeatCount: "count2", // 當組件中與vuex中的字符已經(jīng)出現(xiàn)重復時,使用 repeatCount 來映射 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(){
// 當組件中與vuex中的字符已經(jīng)出現(xiàn)重復時,使用 moduleNameOther 來映射 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ù)組形式
* 當映射的計算屬性的名稱與 與模塊中vuexTest中state 的子節(jié)點名稱相同時,我們也可以給 mapState 傳一個字符串數(shù)組,
* */
...mapState("vuexTest", ["moduleVal", "moduleName"]),
// "vuexTest" 指向模塊vuexTest,"moduleVal"表示store.vuexTest.moduleVal
/**
* 對象形式
*/
// 第一種對象方式
...mapState({
moduleVal: "vuexTest/moduleVal",
moduleNameOther: "vuexTest/moduleName" // 表示 moduleNameOther 映射到vuexTest模塊中moduleName
}),
...mapState("vuexTest", {
moduleVal, // 這種就是moduleVal:"moduleVal", 的簡寫
moduleName: "moduleName",
moduleNameOther: "moduleName", // 當組件中與vuex中的字符已經(jīng)出現(xiàn)重復時,使用 moduleNameOther 來映射 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3 + TypeScript 開發(fā)總結(jié)
本文直接上 Vue3 + TypeScript + Element Plus 開發(fā)的內(nèi)容,感興趣的話一起來看看吧2021-08-08
vue使用keep-alive無效以及include和exclude用法解讀
這篇文章主要介紹了vue使用keep-alive無效以及include和exclude用法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
vue文件上傳讀取文件數(shù)據(jù)進行格式校驗方式
該文章描述了使用Element-UI的`el-upload`組件實現(xiàn)文件上傳前的內(nèi)容校驗,通過`beforeUpload`鉤子讀取并解析文件內(nèi)容,校驗其格式和數(shù)據(jù)有效性,確保符合要求后才進行上傳,詳細介紹了校驗邏輯及上傳流程2026-05-05
vue監(jiān)聽瀏覽器原生返回按鈕,進行路由轉(zhuǎn)跳操作
這篇文章主要介紹了vue監(jiān)聽瀏覽器原生返回按鈕,進行路由轉(zhuǎn)跳操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue2+tracking實現(xiàn)PC端的人臉識別示例
本文主要介紹了vue2+tracking實現(xiàn)PC端的人臉識別示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
在 Vue 3 中設(shè)置 `@` 指向根目錄的幾種常見方法匯總
在 Vue 3 項目開發(fā)中,為了方便管理和引用文件路徑,設(shè)置 @ 指向根目錄是一項常見的需求,下面給大家分享在Vue3中設(shè)置 `@` 指向根目錄的方法匯總,感興趣的朋友一起看看吧2024-06-06

