【发布时间】:2019-02-07 15:39:11
【问题描述】:
我想通过 vuex 读取状态数组。
但它说
“TypeError: 无法读取未定义的属性‘0’”
我猜 [0] 发生错误。
下面是一些代码。
// the code of index.vue
<template>
<div><p>{{ rectangles[0].x }}</p></div>
</template>
<script>
export default {
computed: {
rectangles () {
return this.$store.state.rectangles
}
},
}
</script>
这就是 vuex 的 store。
export default {
state: {
rectangles: [
{
x: 150,
y: 100,
width: 100,
height: 100,
fill: "red",
name: "rect1",
draggable: true
},
{
x: 150,
y: 150,
width: 100,
height: 100,
fill: "green",
name: "rect2",
draggable: true
}
],
},
【问题讨论】:
-
你能把你的代码贴在 jsfiddle、codesandbox 等地方吗?