vuex的使用

1.先安装npm install vuex --save-dev 

2.在src下建立文件夹vuex,在vuex里面写store.js

/**
 * Created by ywz on 2017/8/12.
 */
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  // 定义状态
  state: {
    author: 'Wise Wrong'
  },
  mutations:{
    newAuthor (state,msg){
      state.author=msg;
    }
  }
})

export default store

第三步 在main.js中
import Vuex from 'vuex'
Vue.use(Vuex);
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
render: h => h(App)
});

第四部显示在页面
<div>自拍照{{author}}</div>

computed: {
author()
{
return this.$store.state.author;
}
}

第5步 设置修改
methods: {
setAuthor(){
//this.$store.state.author=this.user;
this.$store.commit('newAuthor',32223);
}
},
原文地址:https://www.cnblogs.com/norm/p/7350024.html