vue2.0中使用vueX
1、安装vueX
npm install vuex --save
2、在src路径下面新建store文件夹,文件夹下面新建index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
name:"邓杰"
},
mutations: {
},
actions: {
}
});
3、在main.js
import store from './store'
new Vue({
el: '#app',
router,
store,//新加的
components: { App },
template: '<App/>'
})
4、使用store仓库中的变量
this.$store.state.name
5、修改或存储state中的变量
this.$store.commit('在mutations中的方法名称',参数)
//将参数传给store下面的方法中,修改变量名
this.$store.commit("setToken",result.data.token)

浙公网安备 33010602011771号