无法访问 Vuex 存储状态

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

Can't access state of store vuex

问题

以下是已翻译的部分:

这是主文件
/main.js

  1. import Vue from 'vue';
  2. import App from './App.vue';
  3. import vuetify from './plugins/vuetify';
  4. import router from './router';
  5. import store from './store/index.js';
  6. Vue.config.productionTip = false;
  7. new Vue({
  8. router,
  9. vuetify,
  10. store,
  11. render: h => h(App)
  12. }).$mount('#app');

这是存储
/store/index.js

  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3. Vue.use(Vuex);
  4. const store = new Vuex.Store({
  5. state: {
  6. appNumber: 0
  7. }
  8. });
  9. export default store;

这是模板
/template

  1. <template>
  2. <div>
  3. {{ appNumber() }}
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. computed: {},
  9. methods: {
  10. appNumber() {
  11. return this.$store.state.appNumber;
  12. }
  13. }
  14. };
  15. </script>

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

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

英文:

Here is of main file
/main.js

  1. import Vue from &#39;vue&#39;;
  2. import App from &#39;./App.vue&#39;;
  3. import vuetify from &#39;./plugins/vuetify&#39;;
  4. import router from &#39;./router&#39;;
  5. import store from &#39;./store/index.js&#39;;
  6. Vue.config.productionTip = false
  7. new Vue({
  8. router,
  9. vuetify,
  10. store,
  11. render: h =&gt; h(App)
  12. }).$mount(&#39;#app&#39;)

Here is store
/store/index.js

  1. import Vue from &#39;vue&#39;;
  2. import Vuex from &#39;vuex&#39;;
  3. Vue.use(Vuex);
  4. const store= new Vuex.Store({
  5. state: {
  6. appNumber: 0
  7. }
  8. })
  9. export default store

Here is template
/template

  1. &lt;template&gt;
  2. &lt;div&gt;
  3. {{appNumber()}}
  4. &lt;/div&gt;
  5. &lt;/template&gt;
  6. &lt;script&gt;
  7. export default {
  8. computed: {
  9. }
  10. ,
  11. methods: {
  12. appNumber() {
  13. return this.$store.state.appNumber;
  14. }
  15. }};
  16. &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 定义为:

  1. getters: {
  2. getAppNumber: state => state.appNumber,
  3. }

更好的方法是,如果您将 ...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:

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

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

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

  1. &lt;template&gt;
  2. &lt;div&gt;
  3. {{ appNumber }}
  4. &lt;/div&gt;
  5. &lt;/template&gt;
  6. &lt;script&gt;
  7. export default {
  8. computed: {
  9. appNumber(){
  10. return this.$store.state.appNumber;
  11. }
  12. }
  13. };
  14. &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.

  1. &lt;template&gt;
  2. &lt;div&gt;
  3. {{ appNumber }}
  4. &lt;/div&gt;
  5. &lt;/template&gt;
  6. &lt;script&gt;
  7. export default {
  8. computed: {
  9. appNumber(){
  10. return this.$store.state.appNumber;
  11. }
  12. }
  13. };
  14. &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:

确定