Element-UI级联选择组件的默认值回显问题
Element-UI级联选择器Cascader,通过v-model保存选择项的值,数据类型为数组,数组元素为所选不同级别选项的值。Vue示例代码如下:html部分:<el-cascader v-model="editForm.goods_cat":options="cateList":props="props"@change="handleCateChange...
·
Element-UI级联选择器Cascader,通过v-model保存选择项的值,数据类型为数组,数组元素为所选不同级别选项的值。Vue示例代码如下:
html部分:
<el-cascader v-model="editForm.goods_cat" :options="cateList" :props="props" @change="handleCateChange">
</el-cascader>
script部分:
data() {
return {
// 添加商品表单数据
editForm: {
goods_name: '',
goods_price: 0,
goods_weight: 0,
goods_number: 0,
goods_cat: [],
pics: [],
attrs: [],
goods_introduce: ''
},
// 级联选择器的数据源
cateList: []
}
}
......
created() {
// 设置级联选择器数据源
this.cateList = this.getCateList();
// 设置级联选择器绑定值
const tempArray = res.data.goods_cat.split(',');
// 这里必须重新赋值为空数组,再赋值,否则v-model不能实现默认值回显
this.editForm.goods_cat = [];
tempArray.forEach(item => {
// item - 0是把数据类型转换为数字,以与cateList 数据类型一致,否则不能正确回显默认值
this.editForm.goods_cat.push(item - 0);
});
}
代码中实现了默认值回显,其中,代码this.editForm.goods_cat = []; 很关键,估计与Vue生命周期及组件解析和二次渲染等有关,或者就是一个bug。
更多推荐
已为社区贡献1条内容
所有评论(0)