vuex 基础用法

1.安装

npm install vuex --save

2.引入

a) 新增store/store.js

b)main.js,引入

3.使用

<button @click="$store.commit('add')">+</button>
<button @click="$store.commit('reduce')">-</button>
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const state = {
  count:1
}
const mutations={
  add(state){
    state.count+=1;
  },
  reduce(state){
    state.count-=1;
  }
}
export default new Vuex.Store({
  state,
  mutations
});
// 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 App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import store from './store/store';

Vue.config.productionTip = false
Vue.use(ElementUI);


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

 

posted on 2020-10-06 11:22  biind  阅读(120)  评论(0编辑  收藏  举报