vuex狀態(tài)管理淺談之mapState用法
一、state
state是什么?
定義:state(vuex) ≈ data (vue)
vuex的state和vue的data有很多相似之處,都是用于存儲(chǔ)一些數(shù)據(jù),或者說狀態(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)聽到,當(dāng)然你也可以通過watch $store去解決這個(gè)問題,那你可以針是一個(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ù).這么說可能很難理解
抽象形容:mapState是state的語法糖,這么說可能你還想罵我,因?yàn)槟愀静涣私馐裁唇凶稣Z法糖,事實(shí)上我說的語法糖有自己的定義,什么是語法糖?我對(duì)語法糖的理解就是,用之前覺得,我明明已經(jīng)對(duì)一種操作很熟練了,并且這種操作也不存在什么問題,為什么要用所謂的"更好的操作",用了一段時(shí)間后,真香!
實(shí)際作用:當(dāng)一個(gè)組件需要獲取多個(gè)狀態(tài)時(shí)候,將這些狀態(tài)都聲明為計(jì)算屬性會(huì)有些重復(fù)和冗余。為了解決這個(gè)問題,我們可以使用 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)和不直觀。如果有些狀態(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ì)算屬性來獲得某個(gè)狀態(tài)
// 通過下面的計(jì)算屬性,就可以在當(dāng)前組件中訪問到count,name,helloName,addNumber 在模板中我們通過大括號(hào)符號(hào)打印出來,當(dāng)然這些可以在vue中使用,比如在watch中監(jiān)聽,在mounted中使用
// 下面的計(jì)算屬性涉及到了vuex管理的狀態(tài)
count() { // 這實(shí)際上是ES6中對(duì)象的簡(jiǎn)化寫法 完整寫法是 count: function { return this.$store.state.count }
return this.$store.state.count
},
name() { // 這實(shí)際上是ES6中對(duì)象的簡(jiǎn)化寫法 完整寫法是 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è)問題
// 當(dāng)一個(gè)組件需要獲取多個(gè)狀態(tài)的時(shí)候,將這些狀態(tài)都聲明為計(jì)算屬性會(huì)有些重復(fù)和冗余。比如上面的name(),count(),helloName(),顯得重復(fù),代碼冗長(zhǎng)
// 為了解決這個(gè)問題,我們可以使用 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ì)算屬性的名稱與 state 的子節(jié)點(diǎn)名稱相同時(shí),我們也可以給 mapState 傳一個(gè)字符串?dāng)?shù)組。
* */
...mapState(["count", "name"]),
/**
* 對(duì)象形式
*/
...mapState({
count, // 這種就是count:"count", 的簡(jiǎn)寫
count1: "count1",
repeatCount: "count2", // 當(dāng)組件中與vuex中的字符已經(jīng)出現(xiàn)重復(fù)時(shí),使用 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(){
// 當(dāng)組件中與vuex中的字符已經(jīng)出現(xiàn)重復(fù)時(shí),使用 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ù)組形式
* 當(dāng)映射的計(jì)算屬性的名稱與 與模塊中vuexTest中state 的子節(jié)點(diǎn)名稱相同時(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)寫
moduleName: "moduleName",
moduleNameOther: "moduleName", // 當(dāng)組件中與vuex中的字符已經(jīng)出現(xiàn)重復(fù)時(shí),使用 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue數(shù)據(jù)操作之點(diǎn)擊事件實(shí)現(xiàn)num加減功能示例
這篇文章主要介紹了vue數(shù)據(jù)操作之點(diǎn)擊事件實(shí)現(xiàn)num加減功能,結(jié)合實(shí)例形式分析了vue.js事件響應(yīng)及數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
Vue實(shí)現(xiàn)兄弟組件間的聯(lián)動(dòng)效果
這篇文章主要介紹了Vue實(shí)現(xiàn)兄弟組件間的聯(lián)動(dòng)效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
vue實(shí)現(xiàn)微信分享朋友圈,發(fā)送朋友的示例講解
下面小編就為大家分享一篇vue實(shí)現(xiàn)微信分享朋友圈,發(fā)送朋友的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
Ruoyi-Vue處理跨域問題同時(shí)請(qǐng)求多個(gè)域名接口(前端處理方法)
跨域問題一直都是很多人比較困擾的問題,這篇文章主要給大家介紹了關(guān)于Ruoyi-Vue處理跨域問題同時(shí)請(qǐng)求多個(gè)域名接口的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
elementUI動(dòng)態(tài)表單?+?el-select?按要求禁用問題
這篇文章主要介紹了elementUI動(dòng)態(tài)表單?+?el-select?按要求禁用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
el-form表單驗(yàn)證的一些實(shí)用方法總結(jié)
表單校驗(yàn)是注冊(cè)環(huán)節(jié)中必不可少的操作,表單校驗(yàn)通過一定的規(guī)則來確保用戶提交數(shù)據(jù)的有效性,下面這篇文章主要給大家介紹了關(guān)于el-form表單驗(yàn)證的一些實(shí)用方法,需要的朋友可以參考下2023-01-01
關(guān)于element-ui日期時(shí)間選擇器選不中12小時(shí)以后的時(shí)間詳解
在之前做個(gè)一個(gè)組件頁面中,引用了element-ui組件的日期選擇器,遇到的一個(gè)小問題,下面這篇文章主要給大家介紹了關(guān)于element-ui日期時(shí)間選擇器選不中12小時(shí)以后時(shí)間的相關(guān)資料,需要的朋友可以參考下2022-08-08
vue3.0中使用element UI表單遍歷校驗(yàn)問題解決
本文主要介紹了vue3.0中使用element UI表單遍歷校驗(yàn)問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

