【问题标题】:Vuex does not commit changes until it's committed manually via dev tools在通过开发工具手动提交之前,Vuex 不会提交更改
【发布时间】:2019-12-01 20:14:40
【问题描述】:

我有一个名为 login.js 的 vuex 存储模块,如下所示

import axios from "axios";
import router from "@/router";

axios.defaults.baseURL = process.env.VUE_APP_API_ENDPOINT;

const state = {
  access_token: localStorage.getItem("access_token") || null,
};

const getters = {
  loggedIn() {
    return (
      state.access_token != null && localStorage.getItem("access_token") != null
    );
  }
};

const mutations = {
  doLogin(state, response) {
    const token = response.authentication_data.access_token;
    localStorage.setItem("access_token", token);
    state.access_token = token;
    router.push("/admin");
  };

const actions = {
  async getToken({ commit }, userdata) {
    let email = userdata.email;
    let password = userdata.password;
    let remember_me = userdata.remember_me;

    await axios
      .post("auth/login", null, {
        params: {
          email,
          password,
          remember_me
        }
      })
      .then(response => {
        if (response.data.meta.status == "true") {
          commit("doLogin", response.data);
        } else {
          alert("wrong password");
        }
      })
      .catch(error => {
        alert(error);
      });
  };

export default {
  state,
  getters,
  actions,
  mutations
};

login.vue 代码

  methods: {
    ...mapActions(["getToken"]),
    login() {
      const userdata = {
        email: this.email,
        password: this.password,
        remember_me: true
      };

      this.getToken(userdata);
    }
  }

登录功能有效,并且第一次设置了令牌,但是当我刷新浏览器时,access_token 消失了。

在浏览器中如下图所示

但如果我通过开发工具提交,它会起作用并且状态会变得持久。

关于 SO 的类似性质的问题,但不回答这个问题。

vuex commit does not commit to store

Vue2 + Vuex Commit Not Committing (without Vue devtools)

Vuex Mutation running, but component not updating until manual commit in vue dev tools

如何通过代码使state.access_token 持久化?问题在于页面刷新我丢失了state.access_token 值。

【问题讨论】:

  • 尝试使用这个github.com/robinvdvleuten/vuex-persistedstate,我在我的商店中使用它>index.js 像这样: import createPersistedState from 'vuex-persistedstate' import Cookies from 'js-cookie' Vue.use(Vuex) // 创建一个新的 store const store = new Vuex.Store({ plugins: [ createPersistedState({ getState: (key) => Cookies.getJSON(key), setState: (key, state) => Cookies.set(key, state, { expires: 3, secure: false }) }) ], actions, getters, mutation, state }) 但你可以使用 localStorage 代替 cookies
  • @Sdpotts93 这不是关于本地存储,而是state.access_token
  • @Techie 如果您希望它在页面刷新后持续存在,您需要将其存储在 本地存储 中。
  • 请参考代码示例。我已经存储了 access_token 并检索了

标签: javascript vue.js vuejs2 vuex vuex-modules


【解决方案1】:

您的代码很好,Vuex 成功地将您的数据“提交”到存储中。您遇到的问题来自 Vuex(开箱即用)不会将您的数据保存在 localStorage 中,我相信这就是您所说的“提交”。正如您的问题在 cmets 中多次提到的那样,您将需要使用第三方软件包(大多数人使用 Vuex-PersistedState,但我更喜欢 Vuex-Persist,因为它更可定制并支持 Typescript)。任何一个都很容易上手。

使用 Vuex-PersistedState,您需要使用新插件更新您的 Vuex 初始化。它应该看起来像这样:

import createPersistedState from 'vuex-persistedstate' // import the package

const store = new Vuex.Store({
  plugins: [createPersistedState()] /// include the imported plugin
})

【讨论】:

  • 这不是关于本地存储,而是state.access_token
  • 要让state.accesstoken 在刷新过程中保持不变,您需要某种本地存储,@Techie。
  • 请参考代码示例。我已经存储了 access_token 并检索了
  • 能分享一下定义和初始化“localStorage”的代码吗?
猜你喜欢
  • 2019-03-22
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 2012-01-24
相关资源
最近更新 更多