Fork me on GitHub

第一次使用vuex

首先在main.js里面使用vuex,state里面声明一些储存的值,通过mutations去改变state里面的状态值。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import Vuex from 'vuex'
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
count: false
},
mutations: {
changeState (state, payload) {
state.count = payload.val
}
}
})
//需要把store放在vue里面
new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App }
})

然后在登陆页面修改store状态值由于vuex只是一个状态值,因此刷新页面的时候改变的值回还原初始化,因此需要结合sessionStorage,代码如下:

1
2
sessionStorage.setItem('loginState',this.userName)
this.$store.commit('changeState',{val: true});

最后在页面判断是否登陆,代码如下

-------------���Ľ�����л�����Ķ�-------------