【发布时间】:2020-04-30 04:31:07
【问题描述】:
如何从状态存储中的对象获取数据以显示在我的组件字段中?
Vuex:
state:
books: [], // this object has id, title, genre
actions:
allBooks(context) {
axios
.get('/api/books')
.then(response => context.commit('SET_BOOKS', response.data))
.catch(error => console.log(error))
},
mutations:
SET_BOOKS(state, books) {
state.books = books
},
组件:
created() {
this.$store.dispatch('allBooks')
},
computed: {
storeBooks() {
return this.$store.state.books
},
},
现在,当我加载书籍页面时,我希望用来自 vuex 存储的数据填充 id、title 和genre 的字段。我无法一一访问对象中的数据。我尝试使用this.books.id = this.$store.books.id,但它不起作用。
【问题讨论】:
-
this.$store.books应该是this.$store.state.books并且您无法访问数组的id;this.$store.books.id,您应该在那里迭代您的列表以访问数组的每个对象 -
我错了,这是一个错字,感谢您的纠正