无法访问 Vuex 存储状态

huangapple go评论50阅读模式
英文:

Can't access state of store vuex

问题

以下是已翻译的部分:

这是主文件
/main.js

import Vue from 'vue';
import App from './App.vue';
import vuetify from './plugins/vuetify';
import router from './router';
import store from './store/index.js';

Vue.config.productionTip = false;

new Vue({
  router,
  vuetify,
  store,
  render: h => h(App)
}).$mount('#app');

这是存储
/store/index.js

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

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    appNumber: 0
  }
});

export default store;

这是模板
/template

<template>
  <div>
    {{ appNumber() }}
  </div>
</template>

<script>
export default {
  computed: {},
  methods: {
    appNumber() {
      return this.$store.state.appNumber;
    }
  }
};
</script>

错误是
"无法读取未定义的属性(读取'state')"

我尝试直接在模板中导入文件store index.js,它可以工作。但我想在main.js文件中声明它,并且可以被所有文件使用。

英文:

Here is of main file
/main.js

import Vue from &#39;vue&#39;;
import App from &#39;./App.vue&#39;;
import vuetify from &#39;./plugins/vuetify&#39;;
import router from &#39;./router&#39;;
import store  from &#39;./store/index.js&#39;;

Vue.config.productionTip = false

new Vue({
  router,
  vuetify,
  store,
  render: h =&gt; h(App)
}).$mount(&#39;#app&#39;)

Here is store
/store/index.js

import Vue from &#39;vue&#39;;
import Vuex from &#39;vuex&#39;;


Vue.use(Vuex);

const store= new Vuex.Store({
  state: {
    appNumber: 0
  }
})

export default store

Here is template
/template

&lt;template&gt;
  &lt;div&gt;
    {{appNumber()}}
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {

  computed: {
    }
  ,
  methods: {
    appNumber() {
      return this.$store.state.appNumber;
  }
}};
&lt;/script&gt;

The error is
" Cannot read properties of undefined (reading 'state') "

I try to access file store index.js by import directly in template and it work. But I want to declare it on main.js file and can use by all file

答案1

得分: 1

通常情况下,我们不直接在存储之外访问状态。通常,我们编写 getter 并像以下方式访问它们:this.$store.getters["getAppNumber"]

在您的存储中,将 getter 定义为:

getters: {
   getAppNumber: state => state.appNumber,
}

更好的方法是,如果您将 ...mapGetters(["getAppNumber"]) 添加到使用组件的 computed 属性中。因为然后您可以使用 this.getAppNumber 简单地访问该值。

英文:

Normally, we don't access the state directly outside of the store. Normally, we write getters and access them as follows: this.$store.getters[&quot;getAppNumber&quot;]

In your store, define the getter as:

getters: {
   getAppNumber: state =&gt; state.appNumber,
}

Even better would be if you would ...mapGetters([&quot;getAppNumber&quot;]) to the computed property of the consuming component. Because then you can simply access the value with this.getAppNumber

答案2

得分: 0

我能看到你正在尝试通过使用方法来访问$store.state

除了在Goga的回答中使用getter之外,如果你想在实例中获取一个响应式计算的值,你应该使用computed

而且你不再需要那个遮罩。

&lt;template&gt;
  &lt;div&gt;
    {{ appNumber }}
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {

  computed: {
     appNumber(){
         return this.$store.state.appNumber;
     }
  }
};
&lt;/script&gt;
英文:

I could see your are trying to access the $store.state by using methods.

Beside using getter in Goga's answer, you are supposed to use computed if you want to get a reactive calculcated value within a instance.

And that you don't need the blanket anymore.

&lt;template&gt;
  &lt;div&gt;
    {{ appNumber }}
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {

  computed: {
     appNumber(){
         return this.$store.state.appNumber;
     }
  }
};
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年6月1日 14:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定