[英]Invalid prop: type check failed for prop "data". Expected Array, got Object
I'm new to vuejs and trying to use the buefy library.我是 vuejs 的新手并尝试使用 buefy 库。
Error :错误 :
Invalid prop: type check failed for prop "data".
无效的道具:道具“数据”的类型检查失败。 Expected Array, got Object
预期数组,得到对象
<template>
<b-table :data="data" :columns="columns"></b-table>
</template>
<script>
export default {
data() {
return {
data: this.data,
columns: [
{
field: 'name',
label: 'Name',
},
]
}
},
mounted() {
axios
.get('/test')
.then(
response => (this.data = response)
)
}
}
</script>
The json content: json 内容:
[{"name":"test"}]
What did I miss?我错过了什么? Thx :)
谢谢 :)
The declaration of data property should be as below:数据属性的声明应如下所示:
data: []
Updated code:更新代码:
<script>
export default {
data() {
return {
data: [],
columns: [
{
field: 'name',
label: 'Name',
},
]
}
},
mounted() {
axios
.get('/test')
.then(
response => (this.data = response)
)
}
}
</script>
As I see Buefy doc here( https://buefy.org/documentation/table#api-view ), Table component expecting data as an Array of Objects.正如我在这里看到的 Buefy 文档( https://buefy.org/documentation/table#api-view ),表组件期望数据作为对象数组。
Axios returns the response in detail, you're assigning response
to this.data
and which object, that's causing this error. Axios 会详细返回响应,您将
response
分配给this.data
以及导致此错误的对象。 So your data will be coming in as response.data
所以你的数据将作为
response.data
data() {
return { data: []}
}
async mounted() {
try {
const { data } = await axios.get('/test')
this.data = data
} catch(err) {
console.err(err)
}
}
In case, if anyone wonders, how to declare a prop with multiple types.以防万一,如果有人想知道,如何声明具有多种类型的道具。 Here's an example.
这是一个例子。
...
props: {
value: {
type: String | Number | Boolean | Object, or [Number, String, Object]
default: ''
}
}
Got it!知道了!
<script>
export default {
data() {
return {
data: [],
columns: [
{
field: 'name',
label: 'Name',
},
]
}
},
mounted() {
axios
.get('/test')
.then(
response => (this.data = response.data)
)
}
}
</script>
Thx :)谢谢 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.