百度上试了很多方法,设置default-expanded-keys不生效,最后使用了下面的方法,亲测有效
const loadNode = async (node: Node, resolve: (data: AreaType[]) => void) => { if (node.level === 0) { const {data} = await getRegionList(areaOptions) if (!props.special) { for (let i = 0; i < 4; i++) { data[i].isLeaf = true; } emit('checkNode', data[0].id, data); await nextTick(() => { treeRef.value?.setCurrentKey(data[0].id); return resolve(data); }); } else { resolve(data) //先接收数据 重点,不然获取不到节点 const node = treeRef.value?.getNode(data[0].id) //获取要展开的父节点,这边业务要求是展开第一个节点,我这边只有层级两层,层级更深的在node.level>=1中调整逻辑 node?.expand() //调用节点的展开,会再次触发loadNode,相当于模拟了一次节点点击 return } } if (node.level >= 1) { areaOptions.parentId = node.data.id; const {data} = await getRegionList(areaOptions) data.forEach(item => { item.isLeaf = true; }); if(props.special){ await nextTick(() => { treeRef.value?.setCurrentKey(data[0].id) emit('checkNode', data[0].id, data); }) } return resolve(data); } };
参考https://blog.csdn.net/lixiang10188/article/details/129179962
标签:node,el,resolve,const,await,tree,Element,data,id From: https://www.cnblogs.com/cstd/p/17798294.html