programing

vuex 스토어 데이터를 리셋/삭제하려면 어떻게 해야 합니까?

prostudy 2022. 8. 27. 09:18
반응형

vuex 스토어 데이터를 리셋/삭제하려면 어떻게 해야 합니까?

인마이/src/store/가지고 있는 폴더actions.js,index.js,mutations.js그리고.state.js다음 정보가 포함되어 있습니다.

actions.syslog

export default {}

index.displaces를 표시합니다.

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

돌연변이.복제

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
  }
}

및 스테이트를 지정합니다.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();
  }
}

하지만 어떤 이유에서인지, 그 데이터는 계속 유지되고 있습니다.

로그아웃으로 스토어를 Import하려고 시도한 적이 있습니까?vue 컴포넌트?

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

그 후, 당신이 작성한 방법으로

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

언급URL : https://stackoverflow.com/questions/46201518/how-can-i-reset-erase-a-vuex-store-data

반응형