vuex简单的使用方法
新建store文件,在store文件下新建index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
user: {},
},
mutations: {
patient (state, payload) {
state.user= payload
},
},
actions: {
},
//计算属性,过滤数据用的
// getters: {
// user: state => state.user,
// }
})
export default store
mian.js挂载
import store from './store'
Vue.prototype.$store = store;
页面赋值
this.$store.user=this.user;
页面取值
{{sex}} //显示对的值
{{$store.user.sex}} //显示undefined
this.sex=this.$store.user.sex