main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import store from './store'
import App from './vuex'
import router from './router'
import vuex from './vuex.vue'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
router,
render: xx=>xx(vuex)
})
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state={
count:44
}
const mutations={
add(state){
state.count++
},
plus(state){
state.count--
}
}
export default new Vuex.Store({
state,
mutations
})
vuex.vue
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state={
count:44
}
const mutations={
add(state){
state.count++
},
plus(state){
state.count--
}
}
export default new Vuex.Store({
state,
mutations
})
注意的是:在computed中定义了count属性,就不需要在data中重新定义了。而且计算属性是基于依赖缓存的,所以达到了数据同步的效果。