简体   繁体   English

无效的道具:道具“数据”的类型检查失败。 预期数组,得到对象

[英]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.

相关问题 我无法正确显示 v-data-table 数据:''无效的道具:道具“项目”的类型检查失败。 预期的数组,得到对象'' - I can't display properly v-data-table data: ''Invalid prop: type check failed for prop “items”. Expected Array, got Object'' 无效的道具:道具“项目”的类型检查失败。 预期的数组,得到字符串 - Invalid prop: type check failed for prop "items". Expected Array, got String 无效的道具:道具“标题”的类型检查失败。 期望值为 ** 的字符串,得到值为 ** 的数字 - Invalid prop: type check failed for prop "title". Expected String with value **, got Number with value ** Vuejs 错误,无效的道具:道具的类型检查失败。 预期日期,得到数值 - Vuejs error, Invalid prop: type check failed for prop. Expected Date, got Number with value [Vue 警告]:无效的道具:道具“已禁用”的类型检查失败。 预期 Boolean,得到 Function - [Vue warn]: Invalid prop: type check failed for prop "disabled". Expected Boolean, got Function [Vue 警告]:无效道具:道具“X”的类型检查失败。 预期,得到字符串 - [Vue warn]: Invalid prop: type check failed for prop "X". Expected , got String [Vue 警告]:无效道具:道具“页面”的类型检查失败。 预期值为 0 的数字,得到值为“”的字符串 - [Vue warn]: Invalid prop: type check failed for prop "page". Expected Number with value 0, got String with value "" 警告:propType失败:使用React的类型为`array` expected`object`的prop - Warning: Failed propType: Invalid prop of type `array` expected `object` with React 道具类型失败:提供给“ForwardRef(DataGrid)”的“object”类型的无效道具“rows”,预期为“array” - Failed prop type: Invalid prop `rows` of type `object` supplied to `ForwardRef(DataGrid)`, expected `array` [Vue 警告]:无效的道具:道具“eventKey”的类型检查失败。 预期的字符串、数字、得到的数组和避免使用非原始值作为键 - [Vue warn]: Invalid prop: type check failed for prop "eventKey". Expected String, Number, got Array AND Avoid using non-primitive value as key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM