这里主要是将store里面的index.js这个文件变得更加有条理性一点,使得整个代码看起来比较清晰
主要的变动在于count.vue,person.vue,index.js
count.vue
<template>
<div>
<h1>当前求和为:{{sum}}</h1>
<h3>当前求和放大10倍为:{{bigSum}}</h3>
<h3>我在{{school}} 学习{{subject}}</h3>
<h3 style="color:red">下方组件的总人数是:{{personList.length}}</h3>
<select v-model.number="n">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button @click="increment(n)">+</button>
<button @click="decrement(n)">-</button>
<button @click="incrementOdd(n)">当前求和为奇数再加</button>
<button @click="incrementWait(n)">等一等再加</button>
</div>
</template>
<script>
import {mapActions, mapGetters, mapMutations, mapState} from 'vuex'
export default {
name:'Count',
data() {
return {
n:1, //用户选择的数字
}
},
computed:{
...mapState('countAbout',['sum','school','subject']),
...mapState('personAbout',['personList']),
...mapGetters('countAbout',['bigSum'])
},
methods: {
//借助mapMutations生成对应的方法,方法中会调用commit去联系mutaions
...mapMutations('countAbout',{increment:'JIA',decrement:'JIAN'}),
// incrementOdd(){
// this.$store.dispatch('jiaOdd',this.n)
// },
// incrementWait(){
// this.$store.dispatch('jiaWait',this.n)
// },
//
...mapActions('countAbout',{incrementOdd:'jiaOdd',incrementWait:'jiaWait'})
},
mounted(){
const x =mapState({he:'sum',xuexiao:'school',xueke:'subject'})
console.log(x)
}
}
</script>
<style>
button{
margin-left: 5px;
}
</style>
person.vue
<template>
<div>
<h1>人员列表</h1>
<h3 style="color:blue">上方组件的求和为{{sum}}</h3>
<input type="text" placeholder="请输入名字" v-model="name">
<button @click="add">添加</button>
<ul>
<li v-for="p in personList" :key="p.id">{{p.name}}</li>
</ul>
</div>
</template>
<script>
import { nanoid } from 'nanoid'
export default {
name:'Person',
data(){
return{
name:''
}
},
computed:{
personList(){
return this.$store.state.personAbout.personList
},
sum(){
return this.$store.state.countAbout.sum
}
},
methods:{
add(){
const personObj={id:nanoid(),name:this.name}
this.$store.commit('personAbout/ADD_PERSON',personObj)
this.name=''
}
}
}
</script>
index.js
//该文件用于创建VUex中最为核心的store
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用vuex插件
Vue.use(Vuex)
//求和相关配置
const countOptions ={
namespaced:true,
actions:{
jiaOdd(context,value){
console.log('actions中的jiaOdd被调用了')
if(context.state.sum % 2){
context.commit('JIA',value)
}
},
jiaWait(context,value){
console.log('actions中的jiaWait被调用了')
setTimeout(() => {
context.commit('JIA',value)
}, 500);
}},
mutations:{
JIA(state,value){
console.log('mutations中的JIA被调用了')
state.sum +=value
},
JIAN(state,value){
console.log('mutations中的JIAN被调用勒')
state.sum -=value
},},
state:{
sum:0, //当前的和
school:'尚硅谷',
subject:'前端',
},
getters:{
bigSum(state){
return state.sum*10
}
}
}
//人员管理相关配置
const personOptions ={
namespaced:true,
actions:{},
mutations:{
ADD_PERSON(state,value){
console.log('mutations中的ADD_PERSON被调用勒')
state.personList.unshift(value)
}},
state:{
personList:[
{id:'001',name:'张三'}
]
},
getters:{}
}
//创建并暴露store
export default new Vuex.Store({
modules:{
countAbout:countOptions,
personAbout:personOptions
}
})