优化此代码 watch( () => currentMenu, () => { if (currentMenu.value.children) { openKeys.value = [ currentMenu.value?.parentId, currentMenu.value?.children[0].parentId, ] selectedKeys.value = [currentMenu.value?.id] } else { openKeys.value = [currentMenu.value?.parentId] selectedKeys.value = [currentMenu.value?.id] } }, { immediate: true, deep: true, }, )
时间: 2024-03-17 18:41:05 浏览: 121
Matlab图形图像属性.doc
可以尝试将代码进行重构,如下所示:
```
watch(currentMenu, () => {
if (!currentMenu.value) {
return;
}
const { parentId, children, id } = currentMenu.value;
openKeys.value = children ? [parentId, children[0].parentId] : [parentId];
selectedKeys.value = [id];
}, {
immediate: true,
deep: true,
});
```
这样可以让代码更加清晰易懂,并且减少了重复的代码。同时,我们也使用了解构赋值来提高代码的可读性。
阅读全文