npm install vuex --save
....../vuex/store.js:
import Vuex from ‘vuex‘;
import Vue from ‘vue‘;
Vue.use(vuex);
const state = { count:1}
const mutations = {
add(state){
state.count++;
},
reduce(state){
state.count--;
}
}
export default new Vuex.Store({ state,mutations });
Count.vue
<template>
<div>
<h2>{{msg}}<h2>
<h3>{{$store.state.count}}</h3>
<p>
<button @click="$store.commit(‘add‘)">+</button>
<button @click="$store.commit(‘reduce‘)">-</button>
</p>
</div>
</template>
<script>
import store from ‘@/vuex/store.js‘
export default{
data(){
return {
msg:"hello vuex"
}
},
store
}
</script>