Vue自定义组件中Props中接收数组或对象

原文:https://www.jianshu.com/p/904551dc6c15

自定义弹框组件时,需要在弹框内展示商品list,所以需要组件中的对应字段接收一个Array数组,默认返回空数组[],直接定义空数组报错,如下所示。

 props: {
    content: {
      type: Array,
      default: []
    },
}

报错信息

[Vue warn]: Invalid default value for prop "content": Props with type Object/Array must use a factory function to return the default value.

根据报错信息提示,Object/Array类型不能直接定义空对象或空数组,必须使用 工厂函数 return 回一个默认值。

使用谷歌直接搜索该报错信息,发现尤大大本人亲自解答了该问题,

https://github.com/vuejs/vue/issues/1032

于是修改成如下代码,问题解决。

 props: {
    content: {
      type: Array,
      // default: function () { return [] }
      default: () => []
    },
}
原文地址:https://www.cnblogs.com/robinunix/p/11383225.html