原文: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: () => [] }, }