在做Drawer嵌套table并默认选中功能时,打开Drawer后发现table的ref操作报错Cannot read property 'toggleRowSelection' of undefined,感觉触发Drawer显示执行table选中逻辑但由于DOM并没有更新完,所以打印this.$refs为空,总结了两种解决办法。
//Drawer打开的Events
//showDrawer() {
//后台接口获取接口以及处理选中行的处理
// this.table = true;
// this.toggleSelection([this.tableData[1], this.tableData[2]]);//选中事件调用
// },
1, toggleSelection(rows) {
let _this =this
if (rows) {
rows.forEach((row) => {
//先用延时器简单验证一下,也能实现效果
setTimeout(function () {
_this.$refs.multipleTable.toggleRowSelection(row);
},100)
});
} else {
this.$refs.multipleTable.clearSelection();
}
},
**2**, toggleSelection(rows) {
if (rows) {
rows.forEach((row) => {
this.$nextTick().then(function () {
_this.$refs.multipleTable.toggleRowSelection(row);
})
});
} else {
this.$refs.multipleTable.clearSelection();
}
},
**//注:Drawer绑定要关闭事件,否则关闭后再打开Drawer后table选中会有问题**
isclose(){
//console.log('你清空了选中')
this.$refs.multipleTable.clearSelection();//清空table的选中
},
分享快乐,哈哈