VUE3.0,DAY59,vuex的mapState、mapGetters、mapActions、mapMutations
作者:互联网
VUE3.0,DAY59
mapState和mapGetters
案例说明
我们使用求和案例,新加一个10倍求和的功能,其中mapstate和mapgetters起到帮我们取数据的作用。
//index.vue的代码
//该文件创建vuex中最为核心的store,为了管理vuex工作原理中的actions、mutations、state
//引入vue
import Vue from 'vue'
//引入vuex
import Vuex from 'vuex'
//准备actions,用于响应组件中的动作
const actions = {
jia: function (miniStore, value) {
miniStore.commit('jia', value)
},
jian: function (miniStore, value) {
miniStore.commit('jian', value)
},
jiShu: function (miniStore, value) {
//把判断和为奇数时在进行求和的逻辑写在这actions中
if (miniStore.state.sum % 2) {
miniStore.commit('jia', value)
}
},
}
//准备mutations,用于操作数据(state)
const mutations = {
jia(state, value) {
state.sum += value
},
jian(state, value) {
state.sum -= value
}
}
//准备state,用于存储数据
const state = {
sum: 0, //当前的和
school: '尚硅谷',
subject: '前端'
}
//准备getters,用于将state中的数据进行加工
const getters = {
bigSum(state) {
return state.sum * 10
}
}
//使用插件
Vue.use(Vuex)
//创建store
const store = new Vuex.Store({
// 传进去一些值,key:value形式
actions: actions,
mutations: mutations,
state: state,
getters: getters
})
//暴露store
export default store
//count.vue代码
<template>
<div>
<h1>当前求和为:{{ he }}</h1>
<h2>当前求和放大10倍为:{{ bigSum }}</h2>
<h3>我在{{ xuexiao }},学习{{ xueke }}</h3>
<select v-model.number="numbers">
<!-- value前边加冒号,就会被当成js表达式处理,如果不加,value值就是字符串形式 -->
<option :value="1">1</option>
<option :value="2">2</option>
<option :value="3">3</option>
</select>
<button @click="JiaFa">+</button>
<button @click="JianFa">-</button>
<button @click="JiShu">当前求和为奇数在加</button>
<button @click="DengXia">等一等再加</button>
</div>
</template>
<script>
import { mapGetters, mapState } from "vuex";
export default {
name: "Count",
data() {
return {
numbers: 1, //用户选择的数字
};
},
computed: {
//程序员自己写的计算属性,这样写之后
// he() {
// return this.$store.state.sum;
// },
// xuexiao() {
// return this.$store.state.school;
// },
// xueke() {
// return this.$store.state.subject;
// },
//借助mapstate生成计算属性,从state中读取数据(对象写法)
...mapState({ he: "sum", xuexiao: "school", xueke: "subject" }),
//借助mapgetters生成计算属性,从getters中读取数据(对象写法)
...mapGetters({bigSum:'bigSum'}),
// bigSum() {
// return this.$store.getters.bigSum;
// },
},
methods: {
JiaFa() {
//调用dispatch,因为$store在this指向的组件实例对象vc身上,dispatch又在store身上
this.$store.dispatch("jia", this.numbers);
},
JianFa() {
this.$store.dispatch("jian", this.numbers);
},
JiShu() {
// if (this.$store.state.sum % 2) {
// this.$store.dispatch("jia", this.numbers);
this.$store.dispatch("jiShu", this.numbers);
// }
},
DengXia() {
setTimeout(() => {
this.$store.dispatch("jia", this.numbers);
}, 500);
},
},
};
</script>
<style>
button {
margin-left: 20px;
}
</style>
输出效果
mapActions和mapMutations
//index.js代码
//该文件创建vuex中最为核心的store,为了管理vuex工作原理中的actions、mutations、state
//引入vue
import Vue from 'vue'
//引入vuex
import Vuex from 'vuex'
//准备actions,用于响应组件中的动作
const actions = {
jia: function (miniStore, value) {
miniStore.commit('jia', value)
},
jian: function (miniStore, value) {
miniStore.commit('jian', value)
},
jiShu: function (miniStore, value) {
//把判断和为奇数时在进行求和的逻辑写在这actions中
if (miniStore.state.sum % 2) {
miniStore.commit('jia', value)
}
},
DengXia: function (miniStore, value) {
setTimeout(() => {
miniStore.commit("jia",value);
}, 500);
},
}
//准备mutations,用于操作数据(state)
const mutations = {
jia(state, value) {
state.sum += value
},
jian(state, value) {
state.sum -= value
}
}
//准备state,用于存储数据
const state = {
sum: 0, //当前的和
school: '尚硅谷',
subject: '前端'
}
//准备getters,用于将state中的数据进行加工
const getters = {
bigSum(state) {
return state.sum * 10
}
}
//使用插件
Vue.use(Vuex)
//创建store
const store = new Vuex.Store({
// 传进去一些值,key:value形式
actions: actions,
mutations: mutations,
state: state,
getters: getters
})
//暴露store
export default store
//count.vue代码
<template>
<div>
<h1>当前求和为:{{ he }}</h1>
<h2>当前求和放大10倍为:{{ bigSum }}</h2>
<h3>我在{{ xuexiao }},学习{{ xueke }}</h3>
<select v-model.number="numbers">
<!-- value前边加冒号,就会被当成js表达式处理,如果不加,value值就是字符串形式 -->
<option :value="1">1</option>
<option :value="2">2</option>
<option :value="3">3</option>
</select>
<!-- 这里加上括号numbers,代表点击事件里传的参数是numbers,如果不加就默认传参是value,鼠标一点,传的就是鼠标点击事件
下边的mapMoutations没法识别要传递的参数是numbers还是鼠标点击事件 -->
<button @click="JiaFa(numbers)">+</button>
<button @click="JianFa(numbers)">-</button>
<button @click="JiShu(numbers)">当前求和为奇数在加</button>
<button @click="DengXia(numbers)">等一等再加</button>
</div>
</template>
<script>
import { mapGetters, mapState, mapActions, mapMutations } from "vuex";
export default {
name: "Count",
data() {
return {
numbers: 1, //用户选择的数字
};
},
computed: {
//借助mapstate生成计算属性,从state中读取数据(对象写法)
...mapState({ he: "sum", xuexiao: "school", xueke: "subject" }),
//借助mapgetters生成计算属性,从getters中读取数据(对象写法)
...mapGetters({ bigSum: "bigSum" }),
},
methods: {
// JiaFa() {
// //调用dispatch,因为$store在this指向的组件实例对象vc身上,dispatch又在store身上
// this.$store.dispatch("jia", this.numbers);
// },
// JianFa() {
// this.$store.dispatch("jian", this.numbers);
// },
// JiShu() {
// // if (this.$store.state.sum % 2) {
// // this.$store.dispatch("jia", this.numbers);
// this.$store.dispatch("jiShu", this.numbers);
// // }
// },
// DengXia() {
// setTimeout(() => {
// this.$store.dispatch("jia", this.numbers);
// }, 500);
// },
//借助mapMutations生成对应的方法,方法中会调用commit去联系mutations
...mapMutations({ JiaFa: "jia", JianFa: "jian" }),
//借助mapActions生成对应的方法,方法中会调用dispatch去联系actions
...mapActions({JiShu:"jiShu",DengXia:'DengXia'}),
},
};
</script>
<style>
button {
margin-left: 20px;
}
</style>
输出效果
小结
标签:DAY59,jia,value,miniStore,mapState,state,dispatch,vuex,store 来源: https://blog.csdn.net/weixin_48681463/article/details/120347272