最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue??vuex配置項(xiàng)和多組件數(shù)據(jù)共享案例分享

 更新時間:2022年04月15日 11:46:19   作者:Errol_King  
這篇文章主要介紹了Vue??vuex配置項(xiàng)和多組件數(shù)據(jù)共享案例分享,文章圍繞Vue?Vuex的相關(guān)資料展開配置項(xiàng)和多組件數(shù)據(jù)共享的案例分享,需要的小伙伴可以參考一下

沒有看過上一篇的同學(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)過加工后再使用時,可以使用 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

首先引入問題。我們在 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)每次取值時都是 store.state.xxx或者$store.getters.xxx,太長了,有的同學(xué)想到了寫計(jì)算屬性來簡化

?? ?<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í)的這個 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ù)(對象寫法)
? ? ?? ?...mapState({he:"sum",xuexiao:"school",xueke:"subject"})
? ? ?? ?//或者
? ? ?? ?//借助mapstate生成計(jì)算屬性,從state中讀取數(shù)據(jù)(數(shù)組寫法)
? ??? ??? ?...mapState(['sum','school',"subject"])
? ?? ?},

其中…這里是 ES6 的語法,舉個例子

?? ?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)于我們在 computed 中增加了開始寫的那一堆方法

同樣 mapGetters

<h3>當(dāng)前求和10倍為:{{ bigSum }}</h3>

computed: {
? ?......
? ? //...mapGetters({bigSum:'bigSum'})
? ? ...mapGetters(['bigSum'])
? },

mapActions、mapMutations

mapMutations 對象寫法  

 ?<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ù)組寫法  

 ?<button @click="JIA(n)">+</button>
? ? <button @click="JIAN(n)">-</button>

?? ?//借助 mapMutations 生成對用的方法,方法中會調(diào)用 commit去聯(lián)系mutations(數(shù)組寫法)
? ? ...mapMutations(["JIA","JIAN"]),

數(shù)組的這種寫法意思是生成方法名為 JIA,commit 的方法名也為 JIA 才能這樣寫,所以調(diào)用時,我們方法名要寫 JIA,同樣的也要把參數(shù)傳進(jìn)去

mapMutations 對象寫法

?? ?<button @click="incrementOdd(n)">當(dāng)前和為奇數(shù)再加</button>
? ? <button @click="incrementWait(n)">等一等再加</button>
? ??
methods:{
?? ?//借助 mapActions 生成對用的方法,方法中會調(diào)用 dispatch 去聯(lián)系 actions(對象寫法)
?? ?...mapActions({incrementOdd:"jiaOdd",incrementWait:"jiaWait"})
}

mapMutations 數(shù)組寫法

?<button @click="jiaOdd(n)">當(dāng)前和為奇數(shù)再加</button>
? ? <button @click="jiaWait(n)">等一等再加</button>
? ? methods:{
?? ?//借助 mapActions 生成對用的方法,方法中會調(diào)用 dispatch 去聯(lián)系 actions(數(shù)組寫法)
? ? ...mapActions(["jiaOdd","jiaWait"])
? ? }

多組件共享數(shù)據(jù)

現(xiàn)在再寫一個 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 寫方法,然后 commit 給 mutations,但是因?yàn)檫壿嫳容^簡單,所以我們直接在 mutations 中寫一個添加人員的方法 ADD_PERSON,然后在 Person.vue 中使用 this.$store.commit提交添加的人員數(shù)據(jù)即可。

先看效果:

完整代碼如下(僅展示改動的代碼):

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;用于存儲數(shù)據(jù)
const state = {
? ? ......
? ? personList:[
? ? ? ? {id:"001",name:"張三"},
? ? ? ? {id:"002",name:"李四"}
? ? ]
}
......

Person.vue

<template>
? <div class="category">
? ? <h1>人員信息</h1>
? ? <input type="text" placeholder="請輸入名字" 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue中使用Element UI的Table組件實(shí)現(xiàn)嵌套表格功能

    Vue中使用Element UI的Table組件實(shí)現(xiàn)嵌套表格功能

    這篇文章主要介紹了Vue中使用Element UI的Table組件實(shí)現(xiàn)嵌套表格功能,演示如何在Vue中使用Element UI的Table組件實(shí)現(xiàn)嵌套表格,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • vue開發(fā)心得和技巧分享

    vue開發(fā)心得和技巧分享

    這篇文章主要為大家分享了vue開發(fā)心得和技巧,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Vue動態(tài)創(chuàng)建注冊component的實(shí)例代碼

    Vue動態(tài)創(chuàng)建注冊component的實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于Vue動態(tài)創(chuàng)建注冊component的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Vue2實(shí)時監(jiān)聽表單變化的示例講解

    Vue2實(shí)時監(jiān)聽表單變化的示例講解

    今天小編就為大家分享一篇Vue2實(shí)時監(jiān)聽表單變化的示例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Vue組件通信方法案例總結(jié)

    Vue組件通信方法案例總結(jié)

    這篇文章主要介紹了Vue組件通信方法案例總結(jié),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • vue的注意規(guī)范之v-if 與 v-for 一起使用教程

    vue的注意規(guī)范之v-if 與 v-for 一起使用教程

    這篇文章主要介紹了vue的注意規(guī)范之v-if 與 v-for 一起使用方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • vue實(shí)現(xiàn)側(cè)邊欄導(dǎo)航效果

    vue實(shí)現(xiàn)側(cè)邊欄導(dǎo)航效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)側(cè)邊欄導(dǎo)航效果,右側(cè)顯示對應(yīng)內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • vue預(yù)覽 pdf、word、xls、ppt、txt文件的實(shí)現(xiàn)方法

    vue預(yù)覽 pdf、word、xls、ppt、txt文件的實(shí)現(xiàn)方法

    這篇文章主要介紹了vue預(yù)覽 pdf、word、xls、ppt、txt文件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 微信小程序Echarts動態(tài)使用及圖表層級踩坑解決方案

    微信小程序Echarts動態(tài)使用及圖表層級踩坑解決方案

    這篇文章主要為大家介紹了微信小程序Echarts動態(tài)使用及圖表層級踩坑解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Vue頁面跳轉(zhuǎn)傳遞參數(shù)及接收方式

    Vue頁面跳轉(zhuǎn)傳遞參數(shù)及接收方式

    這篇文章主要介紹了Vue頁面跳轉(zhuǎn)傳遞參數(shù)及接收方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09

最新評論

德昌县| 崇仁县| 景谷| 特克斯县| 庆安县| 磴口县| 惠安县| 泗水县| 洛浦县| 全南县| 沙田区| 丰镇市| 屯留县| 霍州市| 突泉县| 育儿| 遂川县| 安徽省| 西林县| 余江县| 镇远县| 平远县| 高唐县| 金坛市| 门头沟区| 巴马| 高台县| 永靖县| 武冈市| 桃园市| 天柱县| 梅河口市| 油尖旺区| 曲阳县| 南召县| 迁西县| 繁昌县| 徐州市| 茶陵县| 察雅县| 阿克|