简体   繁体   English

为什么在商店状态更新后视图没有更新? Vuejs

[英]Why is view not getting updated after store state updates ? Vue js

I started a few weeks ago using vue js to build apps.几周前我开始使用 vue js 来构建应用程序。 What i want to do is to build an app where the user get logged.我想要做的是构建一个用户登录的应用程序。 I did it with a function that store the tokens i got from the back-end in a localstorage, then the functions checks if the token is still valid.我使用一个函数将我从后端获得的令牌存储在本地存储中,然后这些函数检查令牌是否仍然有效。 No issue, it works perfectly.没问题,它完美运行。 Then i wanted to use vuex so i can get the status of the user (logged in or not) from anywhere in the app.然后我想使用 vuex,这样我就可以从应用程序的任何地方获取用户的状态(已登录或未登录)。 Now, i'm stuck since a few hours, it's like the view is not getting updated after the states changes.现在,我被困了几个小时,就像状态改变后视图没有更新一样。 I need to refresh my page to actually see correct informations.我需要刷新我的页面才能真正看到正确的信息。 I looked for this issue on the internet, i know there is a vue.set, but my understanding is it can only gets applied to objects ?我在互联网上寻找这个问题,我知道有一个 vue.set,但我的理解是它只能应用于对象?

Please find below the code : I know it looks like it does not make sense, i just display a lot of variables to the screen to build my app : i am displaying current token, the result of the checking of it, it's juste for test purpose.请在代码下方找到:我知道它看起来没有意义,我只是在屏幕上显示很多变量来构建我的应用程序:我正在显示当前令牌,检查结果,它只是为了测试目的。 The issue here is that it does not get updated when states changes, but it used to work when i did not use vuex, with computed properties and methods :这里的问题是当状态改变时它不会更新,但是当我没有使用 vuex 时它曾经工作过,具有计算属性和方法:

In my component在我的组件中

<p>Current token : {{ tokenToCheck }}</p>
    <p>{{ isValid }}</p>
    <p>{{ loggedIn }}</p>
    <p> {{ test }} </p>
    <div class="alert alert-success" v-if="loggedIn"><p>You are logged in</p></div>

store.js商店.js

import Vue from 'vue'
import Vuex from 'vuex'
import jwt from 'jsonwebtoken'

Vue.use(Vuex);

export const store = new Vuex.Store({
    state: {
        tokenToCheck: localStorage.getItem('token'),
        isValid: 'test',
        isLogged: true
    }, 
    getters: {
        isLogged: (state) => {
            return state.isLogged; 
        }
    },
    mutations: {
        CHECK_TOKEN(state) {
            try{
                jwt.verify(state.tokenToCheck, 'RANDOM_TOKEN');
                state.isValid = 'ok';
                state.isLogged = true;
                return true;
            } catch(error) {
                console.log(error);
                console.log(state.tokenToCheck);
                console.log('erreur test token'); 
                state.isValid = "expiry"; 
                state.isLogged = false;
                return false;
            }
        }
    }
});

Computed in my component在我的组件中计算

export default {

  name: 'Home',
  data() {
    return {
      pseudo: '', 
      test: localStorage.getItem('token'), 
      password: '', 
      errorMessage:'',
      token: '',
      isAlert: false
    }
  },
  computed: {
    loggedIn() {
      return this.$store.getters.isLogged; 
    }, 
    isValid(){
      return this.$store.state.isValid;
    },
    tokenToCheck() {
      return this.$store.state.tokenToCHeck;
    }
  },
  mounted() {
    this.$store.commit('CHECK_TOKEN');
  },

Ps: i am not getting any error in the console, and the secret key is not what it is in the post :) Ps:我在控制台中没有收到任何错误,并且密钥不是帖子中的内容:)

The commit should be triggered by a method or inside a life cycle hook and the store state is recommended to be gotten using computed property, in the component script you should have :提交应由方法或在生命周期钩子内触发,建议使用计算属性获取存储状态,在组件脚本中,您应该具有:

computed:{
  loggedIn() {
      return this.$store.getters.isLogged; 
    },
  isValid(){
    return this.$store.state.isValid
   },
  getTokenToCheck(){
     return  this.$store.state.tokenToCheck
  }
},

mounted(){
this.$store.commit('CHECK_TOKEN') 
}

the template will be like :模板将如下所示:

<p>Current token : {{ getTokenToCheck }}</p>
<p>{{isValid }}</p>
<p>{{ loggedIn }}</p>

<div class="alert alert-success" v-if="loggedIn"><p>You are logged in</p></div>

Ok, for those who are interested :好的,对于那些有兴趣的人:

I used vue-storage-watcher npm plugin.我使用了 vue-storage-watcher npm 插件。

It looks like it's working, but i am still opened if more experienced developers have another way to do it.看起来它正在工作,但如果更有经验的开发人员有另一种方法来做到这一点,我仍然愿意。

Thanks again再次感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM