在我的/src/store/
文件夹中,我有actions.js
,index.js
,mutations.js
和state.js
,其中包含以下信息
actions.js
export default {}
index.js
import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import actions from './actions'
import mutations from './mutations'
Vue.use(Vuex)
export default new Vuex.Store({
state,
actions,
mutations
})
mutations.js
export default {
TOGGLE_LOADING (state) {
state.callingAPI = !state.callingAPI
},
TOGGLE_SEARCHING (state) {
state.searching = (state.searching === '') ? 'loading' : ''
},
SET_USER (state, user) {
state.user = user
},
SET_TOKEN (state, token) {
state.token = token
}
}
和state.js
export default {
callingAPI: false,
searching: '',
serverURI: 'http://10.110.1.136:8080',
user: null,
token: null,
userInfo: {
messages: [{1: 'test', 2: 'test'}],
notifications: [],
tasks: []
}
}
现在,当用户登录时,我将状态保持为
this.$store.commit('SET_USER', response.data)
现在,当用户注销时,我运行我的components/logout.vue
文件,其中包含以下代码:
export default {
data() {},
created() {
localStorage.setItem('vuex', null);
this.$store.commit('SET_USER', null);
window.localStorage.clear();
window.sessionStorage.clear();
}
}
但由于某种原因,数据以某种方式持续存在。
您是否尝试将商店导入logout.vue组件?
const store = require('path/to/index.js');
然后在你创建的方法中,尝试
store.default.commit('SET_USER', null);