<template>
<div id="app">
<div id="appaaa">
<h1>这是vuex的示例</h1>
<p>{{$store.state.a.count}}</p>
<p>
<button @click = "addplus">+</button>
<button @click = "subplus">-</button>
</p>
</p>
</div>
</div>
</template>
<script>
//引入mapGetters
import {mapActions} from 'vuex'
export default {
name:'app',
methods:{
...mapActions([
"addplus",
"subplus"
])
}
}
</script>
<style>
<div id="app">
<div id="appaaa">
<h1>这是vuex的示例</h1>
<p>{{$store.state.a.count}}</p>
<p>
<button @click = "addplus">+</button>
<button @click = "subplus">-</button>
</p>
</p>
</div>
</div>
</template>
<script>
//引入mapGetters
import {mapActions} from 'vuex'
export default {
name:'app',
methods:{
...mapActions([
"addplus",
"subplus"
])
}
}
</script>
<style>
</style>
======================
import Vue from 'vue'
import Vuex from 'vuex'
//使用vuex模块
Vue.use(Vuex);
//声明静态常量为4
const state = {
count : 4
};
const mutations = {
add(state,n){
state.count +=n.a;
},
sub(state){
state.count--;
}
};
const actions = {
//2种书写方式
addplus(context){ //可以理解为代表了整个的context
context.commit('add',{a:10})
},
subplus({commit}){
commit('sub');
}
};
//1定义模块组
var moduleA = {
state,
mutations,
actions
};
//导出一个模块
export default new Vuex.Store({
//2声明模块组
modules:{
a:moduleA
}
//3在App.vue中调用
})