Add & modify files to realize vuex modularization

This commit is contained in:
Miku-he 2022-06-09 16:10:30 +08:00
parent 3794916e17
commit d8bb6ddda4
6 changed files with 71 additions and 65 deletions

View File

@ -1,5 +1,5 @@
{ {
"name": "vuex_demo1_1", "name": "vuex_demo1",
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"scripts": { "scripts": {

View File

@ -1,11 +1,11 @@
<!--moguyun--> <!--moguyun-->
<template> <template>
<div> <div>
<h3>当前最新的count值为{{$store.state.count}}</h3> <h3>当前最新的count值为{{$store.state.countOptions.count}}</h3>
<button @click="addcount">+1</button> <button @click="addcount">+1</button>
<button @click="btnhandle">+1 Async</button> <button @click="btnhandle">+1 Async</button>
<button @click="btnhandle1">+5 Async</button> <button @click="btnhandle1">+5 Async</button>
<h3>{{$store.getters.showNum}}</h3> <h3>{{$store.getters['countOptions/showNum']}}</h3>
</div> </div>
</template> </template>
@ -18,16 +18,17 @@ export default {
}, },
methods: { methods: {
addcount(){ addcount(){
this.$store.commit('add') this.$store.commit('countOptions/add')
}, },
//count1 //count1
btnhandle(){ btnhandle(){
//dispatchaction //dispatchaction
this.$store.dispatch('addAsync') this.$store.dispatch('countOptions/addAsync')
}, },
btnhandle1(){ btnhandle1(){
this.$store.dispatch('addNAsync',5) this.$store.dispatch('countOptions/addNAsync',5)
} }
} },
} }
</script> </script>

View File

@ -14,11 +14,11 @@ export default {
}, },
methods:{ methods:{
divide(num){ divide(num){
this.$store.commit('divide',num) this.$store.commit('countOptions/divide',num)
} }
}, },
computed:{ computed:{
...mapState(['count']) ...mapState('countOptions',['count'])
} }
} }
</script> </script>

View File

@ -16,12 +16,12 @@ export default {
return{ } return{ }
}, },
methods:{ methods:{
...mapMutations(['subN','xc']), ...mapMutations('countOptions',['subN','xc']),
...mapActions(['subNAsync']), ...mapActions('countOptions',['subNAsync']),
}, },
computed:{ computed:{
...mapState(['count']), ...mapState('countOptions',['count']),
...mapGetters(['showNum','showNum1']) ...mapGetters('countOptions',['showNum','showNum1'])
} }
} }
</script> </script>

View File

@ -0,0 +1,55 @@
export default {
namespaced: true,
state: {
//存放全局共享数据
count:0
},
getters: {
showNum(state){
let newcount = state.count *2
return '测试getters,更新的数量为count*2: '+ newcount
},
showNum1(state){
let newcount = state.count *3
return '测试getters,更新的数量为为count*3: '+ newcount
}
},
mutations: {
//变更store中的数据
add(state){
state.count++
},
addN(state,step){
state.count+=step
},
sub(state){
state.count--
},
subN(state,step){
state.count-=step
},
xc(state,step){
state.count=state.count * step
},
divide(state,num){
state.count = state.count / num
}
},
actions: {
addAsync(context){
setTimeout(()=>{
context.commit('add')
},1000)
},
addNAsync(context,step){
setTimeout(()=>{
context.commit('addN',step)
},1000)
},
subNAsync(context,step){
setTimeout(()=>{
context.commit('subN',step)
},1000)
}
},
}

View File

@ -2,61 +2,11 @@
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import countOptions from '@/store/count'
Vue.use(Vuex) Vue.use(Vuex)
export default new Vuex.Store({ export default new Vuex.Store({
state: {
//存放全局共享数据
count:0
},
getters: {
showNum(state){
let newcount = state.count *2
return '测试getters,更新的数量为count*2: '+ newcount
},
showNum1(state){
let newcount = state.count *3
return '测试getters,更新的数量为为count*3: '+ newcount
}
},
mutations: {
//变更store中的数据
add(state){
state.count++
},
addN(state,step){
state.count+=step
},
sub(state){
state.count--
},
subN(state,step){
state.count-=step
},
xc(state,step){
state.count=state.count * step
},
divide(state,num){
state.count = state.count / num
}
},
actions: {
addAsync(context){
setTimeout(()=>{
context.commit('add')
},1000)
},
addNAsync(context,step){
setTimeout(()=>{
context.commit('addN',step)
},1000)
},
subNAsync(context,step){
setTimeout(()=>{
context.commit('subN',step)
},1000)
}
},
modules: { modules: {
countOptions
} }
}) })