报错信息:
报错信息处代码:
父组件 index.vue :
子组件 cell.vue :
报错分析:
这个报错是由于在使用uniapp中的Vue框架进行父传子操作时,给一个类型为 Object 或 Array 的 prop(属性)设置了无效的默认值。在Vue中,类型为Object或Array的prop必须使用一个工厂函数来返回默认值。
解决方案一:在你的组件中定义一个名为props
的对象,并为list
属性设置一个工厂函数来返回默认值。修改子组件内的代码 (cell.vue):
export default {
props: {
selectedIndex: { //文章中的报错跟selectedIndex没有关系,这一部分保持不动
type: Number,
default: 0
},
list: {
type: Array,
default: function() { //这里默认return一个数组,即可解决上述的报错问题
return [];
}
}
}
}
解决方案二:不修改子组件内的代码,在父组件的 cell 标签中加入 :list="list" 不要忘了先在当前页面的data里面先定义list为 [ ] ,修改后代码如下:
<cell ref="task" :list="list" :selectedIndex="selectedIndex">
</cell>