const isExpand = ref(false);
const expandRowKey =ref([''])
function handleExpand() {
nextTick(()=>{
isExpand.value = !isExpand.value;
if( isExpand.value){
expandRowKey.value = ['1']
}else{
expandRowKey.value = ['']
}
})
}
_____________________________________
<el-table
v-loading="loading"
:data="menuList"
highlight-current-row
row-key="id"
:expand-row-keys="expandRowKey"
@row-click="onRowClick"
:tree-props="{
children: 'children',
hasChildren: 'hasChildren',
}"
:header-cell-style="{
background: 'var(--el-fill-color-light)',
color: 'var(--el-text-color-primary)',
}"
:default-expand-all="isExpand"
>
其实很简单 :default-expand-all="isExpand" 控制是否全部展开 ,但是el-table-tree是否展开又受:expand-row-keys="expandRowKey" 控制 ,所以我们将两个参数值都动态控制就能实现啦