小编JIm*_*lls的帖子

Vue.js 数组中的对象在存储中的数据中未定义

抱歉,如果这真的很明显,但我 \xe2\x80\x99m 是 Vue 新手,需要一些帮助。

\n\n

我\xe2\x80\x99m 从我的商店中获取一组数据(帖子),并尝试控制台仅记录数组中的一个对象,但它\xe2\x80\x99s 每次都显示未定义。如果我控制台记录整个数组,它会返回正常。

\n\n

I\xe2\x80\x99m 猜测这与创建的钩子中的 console.log 之前未加载数据有关?我\xe2\x80\x99已经尝试了我能做的一切,但它\xe2\x80\x99让我发疯。任何帮助表示赞赏(下面的简化代码)。

\n\n
<script>\n        export default {\n          components: {},\n\n     computed: {\n            posts() {\n              return this.$store.state.posts;\n            }\n          },\n          created() {\n            this.$store.dispatch("getPosts");\n           console.log(this.posts[0])\n           },\n        };\n        </script>\n\n\n//Store code Below\n\nexport const state = () => ({\n      posts: [],\n    })\n\n    export const mutations = {\n      updatePosts: (state, posts) => {\n        state.posts = posts\n      }\n    }\n\n    export const actions = {\n      async getPosts({\n        state,\n        commit,\n        dispatch\n      }) {\n        if (state.posts.length) return\n\n        try {\n          let …
Run Code Online (Sandbox Code Playgroud)

arrays object vue.js vuex nuxt.js

3
推荐指数
1
解决办法
2938
查看次数

标签 统计

arrays ×1

nuxt.js ×1

object ×1

vue.js ×1

vuex ×1