vue3中antd tree组件使用expandedKeys进行展开后无法收起
时间: 2023-08-02 08:06:34 浏览: 298
用Vue3写的目录组件,支持搜索过滤,展开收起
您好!针对您的问题,可以尝试在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>
```
阅读全文