简体   繁体   English

在 VueJS 项目上使用 Vuex 得到错误“TypeError: this.$state is undefined”

[英]Getting error "TypeError: this.$state is undefined" using Vuex on VueJS project

The goal is auth the user from Firebase and store the state using Vuex, when app is opened I use the state to know if user is logged in, and use it to more actions.目标是验证来自 Firebase 的用户并使用 Vuex 存储状态,当应用程序打开时,我使用状态来了解用户是否已登录,并将其用于更多操作。 I need get a state to populate a variable on data() method on App.vue file, but it apparentely not getting the correct reference.我需要一个状态来填充App.vue文件的data()方法上的变量,但它显然没有得到正确的引用。 When the page is loaded the follow error occurs:加载页面时出现以下错误:

[Vue warn]: Error in data(): "TypeError: this.$state is undefined"
found in

---> <App> at src/App.vue
       <Root>

and

[Vue warn]: Property or method "isLoggedIn" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <App> at src/App.vue
       <Root>

App.vue应用程序.vue

<template>
  <v-app>
    <router-view v-if="isLoggedIn"></router-view>
    <login v-else />
  </v-app>
</template>

<script>
import login from "@/views/Login";

export default {
  name: "App",
  components: {
    login
  },
  data() {
    return {
      isLoggedIn: this.$state.getters.user != null
    };
  }
};
</script>

<style>
</style>

store/index.js存储/index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: null,
    status: null,
    error: null
  },
  mutations: {
    setUser(state, payload) {
      this.user = payload
    },

    removeUser(state) {
      this.user = null
    },

    setStatus(state, payload) {
      this.status = payload
    },

    setError(state, payload) {
      this.error = payload
    }
  },
  actions: {
    setUserAction({ commit }, payload) {
      commit('setUser', payload)
      commit('setStatus', 'success')
      commit('setError', null)
    }

  },
  modules: {
  },
  getters: {
    status(state) {
      return state.status
    },

    user(state) {
      return state.user
    },

    error(state) {
      return state.error
    }
  }
})

You should already have something like this to initialize vue and vuex你应该已经有这样的东西来初始化 vue 和 vuex

import Vue from 'vue'
import App from './App' // src/App.vue
import store from './store' // store/index.js

Vue.use(store)

new Vue({ store, render: h => h(App) }).$mount('#app') // notice the "store" attribute here

So in component, it should be available in this.$store.state所以在组件中,它应该在this.$store.state中可用

And also, as @skirtle has pointed out, use computed if you want it to reactive with vuex而且,正如@skirtle 所指出的,如果您希望它与 vuex 反应,请使用计算

computed: {
  isLoggedIn () {
     return  this.$store.getters.user != null // remove .state
     // or you can use the state directly
     // return  this.$store.state.user != null
  }
}

In vuex, you should make change to state with a mutation.在 vuex 中,您应该使用突变来更改状态。 I see you already have setUser我看到你已经有 setUser

setUser(state, payload) {
  state.user = payload // change this.user to state.user
},

you can then change the state in component with this.$store.commit('setUser', this.user)然后,您可以使用this.$store.commit('setUser', this.user)更改组件中的状态

And I see you have made an action setUserAction to do 3 commits, so you can just call it in component with this.$store.dispatch('setUserAction', this.user)我看到你已经做了一个动作setUserAction来做 3 次提交,所以你可以用this.$store.dispatch('setUserAction', this.user)在组件中调用它

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

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