Vue??vuex配置項(xiàng)和多組件數(shù)據(jù)共享案例分享
沒(méi)有看過(guò)上一篇的同學(xué)可以查看: Vue Vuex搭建vuex環(huán)境及vuex求和案例分享
getters 配置項(xiàng)
index.js 中增加 getters 配置項(xiàng)
//準(zhǔn)備getters,用于將state中的數(shù)據(jù)進(jìn)行加工
const getters = {
? ? bigSum(state){
? ? ? ? return state.sum*10
? ? }
}
//創(chuàng)建并暴露store
export default new Vuex.Store({
? ? ......
? ? getters,
});
Count.vue 中使用
<h1>當(dāng)前求和10倍為:{{$store.getters.bigSum}}</h1>

總結(jié):
- 1.概念:當(dāng) state 中的數(shù)據(jù)需要經(jīng)過(guò)加工后再使用時(shí),可以使用 getters 加工
- 2.在 store.js中追加 getters 配置
//準(zhǔn)備getters,用于將state中的數(shù)據(jù)進(jìn)行加工
const getters = {
? ? bigSum(state){
? ? ? ? return state.sum*10
? ? }
}
//創(chuàng)建并暴露store
export default new Vuex.Store({
? ? ......
? ? getters,
});
3.組件中讀取數(shù)據(jù):$store.getters.bigSum
mapState、mapGetters
首先引入問(wèn)題。我們?cè)?index.js 中增加學(xué)校和學(xué)科字段
const state = {
? ? sum: 0,//當(dāng)前和
? ? school:"三里屯小學(xué)",
? ? subject:"Vue",
}
Count.vue 中使用
?? ?<h1>當(dāng)前求和為:{{$store.state.sum}}</h1>
? ? <h3>當(dāng)前求和10倍為:{{$store.getters.bigSum}}</h3>
? ? <h3>我在:{{$store.state.school}}學(xué)習(xí){{$store.state.subject}}</h3>查看下當(dāng)前效果:

我們發(fā)現(xiàn)每次取值時(shí)都是 store.state.xxx或者$store.getters.xxx,太長(zhǎng)了,有的同學(xué)想到了寫(xiě)計(jì)算屬性來(lái)簡(jiǎn)化
?? ?<h1>當(dāng)前求和為:{{he}}</h1>
? ? <h3>當(dāng)前求和10倍為:{{$store.getters.bigSum}}</h3>
? ? <h3>我在:{{xuexiao}}學(xué)習(xí){{xueke}}</h3>
computed:{
? ? he(){
? ? ? return this.$store.state.sum
? ? },
? ? xuexiao(){
? ? ? return this.$store.state.school
? ? },
? ? xueke(){
? ? ? return this.$store.state.subject
? ? }
? }
當(dāng)然可以使用要學(xué)習(xí)的這個(gè) mapState
? ?<h1>當(dāng)前求和為:{{he}}</h1>
? ? <h3>當(dāng)前求和10倍為:{{$store.getters.bigSum}}</h3>
? ? <h3>我在:{{xuexiao}}學(xué)習(xí){{xueke}}</h3>
?? ?computed:{
? ? ?? ?//借助mapstate生成計(jì)算屬性,從state中讀取數(shù)據(jù)(對(duì)象寫(xiě)法)
? ? ?? ?...mapState({he:"sum",xuexiao:"school",xueke:"subject"})
? ? ?? ?//或者
? ? ?? ?//借助mapstate生成計(jì)算屬性,從state中讀取數(shù)據(jù)(數(shù)組寫(xiě)法)
? ??? ??? ?...mapState(['sum','school',"subject"])
? ?? ?},
其中…這里是 ES6 的語(yǔ)法,舉個(gè)例子
?? ?let obj1 = {x:100,y:200}
? ? ? ? let obj2 = {
? ? ? ? ? ? a:1,
? ? ? ? ? ? ...obj1,
? ? ? ? ? ? b:2,
? ? ?}
? ? ?console.log(obj2);

所以...mapState({he:"sum",xuexiao:"school",xueke:"subject"})就相當(dāng)于我們?cè)?computed 中增加了開(kāi)始寫(xiě)的那一堆方法
同樣 mapGetters
<h3>當(dāng)前求和10倍為:{{ bigSum }}</h3>
computed: {
? ?......
? ? //...mapGetters({bigSum:'bigSum'})
? ? ...mapGetters(['bigSum'])
? },mapActions、mapMutations
mapMutations 對(duì)象寫(xiě)法
?<button @click="increment(n)">+</button>
? ? <button @click="decrement(n)">-</button>
?? ?methods: {
? ? /*increment() {
? ? ? this.$store.commit("JIA", this.n);
? ? },
? ? decrement() {
? ? ? this.$store.commit("JIAN", this.n);
? ? },*/
? ? ...mapMutations({"increment":"JIA","decrement":"JIAN"}),
? ?......
? }
mapMutations 數(shù)組寫(xiě)法
?<button @click="JIA(n)">+</button> ? ? <button @click="JIAN(n)">-</button> ?? ?//借助 mapMutations 生成對(duì)用的方法,方法中會(huì)調(diào)用 commit去聯(lián)系mutations(數(shù)組寫(xiě)法) ? ? ...mapMutations(["JIA","JIAN"]),
數(shù)組的這種寫(xiě)法意思是生成方法名為 JIA,commit 的方法名也為 JIA 才能這樣寫(xiě),所以調(diào)用時(shí),我們方法名要寫(xiě) JIA,同樣的也要把參數(shù)傳進(jìn)去

mapMutations 對(duì)象寫(xiě)法
?? ?<button @click="incrementOdd(n)">當(dāng)前和為奇數(shù)再加</button>
? ? <button @click="incrementWait(n)">等一等再加</button>
? ??
methods:{
?? ?//借助 mapActions 生成對(duì)用的方法,方法中會(huì)調(diào)用 dispatch 去聯(lián)系 actions(對(duì)象寫(xiě)法)
?? ?...mapActions({incrementOdd:"jiaOdd",incrementWait:"jiaWait"})
}mapMutations 數(shù)組寫(xiě)法
?<button @click="jiaOdd(n)">當(dāng)前和為奇數(shù)再加</button>
? ? <button @click="jiaWait(n)">等一等再加</button>
? ? methods:{
?? ?//借助 mapActions 生成對(duì)用的方法,方法中會(huì)調(diào)用 dispatch 去聯(lián)系 actions(數(shù)組寫(xiě)法)
? ? ...mapActions(["jiaOdd","jiaWait"])
? ? }多組件共享數(shù)據(jù)
現(xiàn)在再寫(xiě)一個(gè) Person 組件,展示人員信息。要完成 Person 組件展示剛才 Count 組件中的 sum 值。而 Count 組件展示人員信息
我們首先完成 Person 組件的人員展示和添加。首先在 index.js 中的 state 中存入 personList 做為要展示的人員數(shù)據(jù)。然后在 Person.vue 中使用 v-for 循環(huán)出人員數(shù)據(jù)
然后實(shí)現(xiàn)添加人員方法。正常應(yīng)該在 index.js 中的 actions 寫(xiě)方法,然后 commit 給 mutations,但是因?yàn)檫壿嫳容^簡(jiǎn)單,所以我們直接在 mutations 中寫(xiě)一個(gè)添加人員的方法 ADD_PERSON,然后在 Person.vue 中使用 this.$store.commit提交添加的人員數(shù)據(jù)即可。
先看效果:

完整代碼如下(僅展示改動(dòng)的代碼):
index.js
......
//準(zhǔn)備mutations;用于操作數(shù)據(jù)(state)
const mutations = {
? ? ......
? ? ADD_PERSON(state,value){
? ? ? ? console.log("mutations中的ADD_PERSON被調(diào)用了",state,value);
? ? ? ? state.personList.unshift(value)
? ? }
}
//準(zhǔn)備state;用于存儲(chǔ)數(shù)據(jù)
const state = {
? ? ......
? ? personList:[
? ? ? ? {id:"001",name:"張三"},
? ? ? ? {id:"002",name:"李四"}
? ? ]
}
......Person.vue
<template>
? <div class="category">
? ? <h1>人員信息</h1>
? ? <input type="text" placeholder="請(qǐng)輸入名字" v-model="name"/>
? ? <button @click="add">添加</button>
? ? <ul>
? ? ? <li v-for="person in personList" :key="person.id">{{person.name}}</li>
? ? </ul>
? </div>
</template>
<script>
import {nanoid} from "nanoid"
export default {
? name: "Person",
? data(){
? ? return{
? ? ? name:""
? ? }
? },
? methods:{
? ? add(){
? ? ? const personObj = {id:nanoid(),name:this.name}
? ? ? this.$store.commit("ADD_PERSON",personObj)
? ? }
? },
? computed:{
? ? personList(){
? ? ? return this.$store.state.personList;
? ? }
? }
}
</script>
<style scoped>
select, button {
? margin-right: 5px;
}
</style>App.vue 中引入組件并使用
<template>
? <div>
? ? <Count/>
? ? <hr>
? ? <Person/>
? </div>
</template>
<script>
import Count from "@/components/Count";
import Person from "@/components/Person";
export default {
? name: 'App',
? components: {Count,Person},
}
</script>
<style>
</style>下面實(shí)現(xiàn)數(shù)據(jù)共享,我們讓 Count 組件展示 Person 組件中總?cè)藬?shù),Person 組件展示 Count 組件的求和數(shù)
修改 Count 組件
<h3 style="color: red">Person組件的總?cè)藬?shù)為{{personList.length}}</h3>
<script>
......
export default {
? ......
? computed: {
? ...
? ? ...mapState(['sum','school',"subject","personList"])
? ? ...
? }
? ......
}
</script>修改 Person 組件
<h3 style="color: red">Count組件求和為{{sum}}</h3>
<script>
......
export default {
? ......
? computed:{
? ? ......
? ? sum(){
? ? ? return this.$store.state.sum;
? ? }
? }
}
</script>查看效果:

到此這篇關(guān)于Vue vuex配置項(xiàng)和多組件數(shù)據(jù)共享案例分享的文章就介紹到這了,更多相關(guān)Vue Vuex案例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue富文本插件(quill-editor)的使用及說(shuō)明
這篇文章主要介紹了Vue富文本插件(quill-editor)的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-02-02
Vue.js常用指令之循環(huán)使用v-for指令教程
這篇文章主要跟大家介紹了關(guān)于Vue.js常用指令之循環(huán)使用v-for指令的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
vue制作點(diǎn)擊切換圖片效果的詳細(xì)思路與步驟
這篇文章主要給大家介紹了關(guān)于vue制作點(diǎn)擊切換圖片效果的詳細(xì)思路與步驟,圖片切換是一個(gè)很經(jīng)典的Vue入門(mén)學(xué)習(xí)案例,在你學(xué)習(xí)完一些基本的v-指令后,你可以嘗試去寫(xiě)一個(gè)簡(jiǎn)單的demo去鞏固和熟悉這些指令的使用方法,需要的朋友可以參考下2023-11-11
vue2 自定義動(dòng)態(tài)組件所遇到的問(wèn)題
這篇文章主要介紹了vue2 自定義 動(dòng)態(tài)組件的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-06-06
Element-ui設(shè)置el-table表頭全選框隱藏或禁用
這篇文章主要給大家介紹了關(guān)于Element-ui設(shè)置el-table表頭全選框隱藏或禁用的相關(guān)資料,文中手把手教你實(shí)現(xiàn)el-table實(shí)現(xiàn)跨表格禁用選項(xiàng),需要的朋友可以參考下2023-07-07
vue實(shí)現(xiàn)發(fā)送短信倒計(jì)時(shí)和重發(fā)短信功能的示例詳解
這篇文章主要給大家介紹了vue實(shí)現(xiàn)發(fā)送短信倒計(jì)時(shí)和重發(fā)短信功能的相關(guān)知識(shí),文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
element UI 2.15.13與vue2.0表格勾選回顯關(guān)鍵demo
這篇文章主要為大家介紹了element UI 2.15.13與vue2.0表格勾選回顯關(guān)鍵demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

