Add & modify files to realize vuex modularization
This commit is contained in:
parent
3794916e17
commit
d8bb6ddda4
@ -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": {
|
||||||
|
@ -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')
|
||||||
},
|
},
|
||||||
//异步让count自增1
|
//异步让count自增1
|
||||||
btnhandle(){
|
btnhandle(){
|
||||||
//dispatch函数专门用来触发action
|
//dispatch函数专门用来触发action
|
||||||
this.$store.dispatch('addAsync')
|
this.$store.dispatch('countOptions/addAsync')
|
||||||
},
|
},
|
||||||
btnhandle1(){
|
btnhandle1(){
|
||||||
this.$store.dispatch('addNAsync',5)
|
this.$store.dispatch('countOptions/addNAsync',5)
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -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>
|
@ -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>
|
55
vuex_demo1/src/store/count.js
Normal file
55
vuex_demo1/src/store/count.js
Normal 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)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
@ -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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user