vue3中antd tree组件使用expandedKeys进行展开后无法收起
时间: 2023-08-02 22:06:34 浏览: 268
vue实现点击展开点击收起效果
您好!针对您的问题,可以尝试在Vue3中使用antd tree组件时,给每个节点添加一个可展开属性`isLeaf`,然后在`onSelect`事件中判断当前节点是否可展开,如果是则将该节点的`expanded`属性设置为`!expanded`,这样就可以实现展开和收起的功能了。具体实现可以参考以下代码:
```
<template>
<a-tree :treeData="treeData" :expandedKeys="expandedKeys" @select="onSelect"></a-tree>
</template>
<script>
import { ref } from 'vue';
import { Tree } from 'ant-design-vue';
export default {
components: {
'a-tree': Tree
},
setup() {
const treeData = ref([
{
title: 'Node1',
key: '1',
children: [
{
title: 'Child Node1',
key: '1-1',
isLeaf: true
},
{
title: 'Child Node2',
key: '1-2',
isLeaf: true
}
]
},
{
title: 'Node2',
key: '2',
children: [
{
title: 'Child Node3',
key: '2-1',
isLeaf: true
},
{
title: 'Child Node4',
key: '2-2',
isLeaf: true
}
]
}
]);
const expandedKeys = ref([]);
const onSelect = (selectedKeys, info) => {
const { node } = info;
if (!node.isLeaf) {
node.expanded = !node.expanded;
expandedKeys.value = node.expanded
? expandedKeys.value.concat(node.key)
: expandedKeys.value.filter((key) => key !== node.key);
}
};
return {
treeData,
expandedKeys,
onSelect
};
}
};
</script>
```
阅读全文