2017-09-13 1922 views
1

在我/src/store/文件夹我有actions.jsindex.jsmutations.jsstate.js它包含以下信息如何重置/擦除vuex存储数据?

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(); 
    } 
} 

但由于某种原因,数据以某种方式持续存在。

+0

'但由于某些原因,该数据以某种方式persisted.'持久保存在哪里?在'state.user'或localStorage中? – Pradeepb

+0

它坚持'state.user' – hidar

+0

我不确定最新错误,同样的作品[这里](http://jsfiddle.net/zfab6tzp/22/) – Pradeepb

回答

1

您是否尝试将您的商店导入到logout.vue组件?

const store = require('path/to/index.js'); 

然后在创建的方法,尝试

store.default.commit('SET_USER', null); 
+0

是的,我试过了。它不起作用 – hidar