if (node.type === 'model') { // 删除当前打开的页面 if (node.id === route?.query?.modelId) { router.push({ path: route.path }); emit('deleteCurrent'); } } else { // 当前页面属于当前删除节点的子节点 if (keys?.includes(route.query.modelId as string)) { router.push({ path: route.path }); emit('deleteCurrent'); } }简化代码
时间: 2024-02-14 14:04:14 浏览: 24
这段代码是一个条件语句,根据节点的类型和ID来删除当前打开的页面,或者删除当前节点的子节点对应的页面。可以进行以下的简化:
```
if (node.type === 'model' && node.id === route?.query?.modelId || keys?.includes(route.query.modelId as string)) {
router.push({ path: route.path });
emit('deleteCurrent');
}
```
这样可以将两个分支的重复代码合并到一起,使代码更加简洁易懂。
相关问题
if (node.type === 'model' && node.id !== currentNodeKey.value) { return; } if (node.type !== 'model' && !keys?.includes(currentNodeKey.value)) { return; } if (node.type === 'model' && node.id === currentNodeKey.value) { currentNodeKey.value = ''; // 取消选中状态 getTree().setSelectedKeys([]); } if (node.type !== 'model' && keys?.includes(node.id)) { currentNodeKey.value = ''; } 简化代码
可以将这段代码进一步简化,去掉冗余的条件判断和重复的代码:
```
if (node.type === 'model') {
if (node.id === currentNodeKey.value) {
currentNodeKey.value = '';
getTree().setSelectedKeys([]);
}
else {
return;
}
}
else {
if (!keys?.includes(currentNodeKey.value)) {
return;
}
else {
currentNodeKey.value = '';
}
}
```
这样就可以用更简洁的代码实现同样的功能。
if (node.type === 'script' && node.id !== currentScriptKey.value) { return; } // 删除主题和分类:当前选中节点不属于当前删除节点的子节点 if (node.type !== 'script' && !keys?.includes(currentScriptKey.value)) { return; } if (node.type === 'script' && node.id === currentScriptKey.value) { currentScriptKey.value = ''; // 取消选中状态 getTree().setSelectedKeys([]); } // 删除主题和分类:当前选中的脚本节点属于删除节点的子节点时,清空key if (node.type !== 'script' && keys?.includes(node.id)) { currentScriptKey.value = ''; }简化代码
以下是简化后的代码:
```
if (node.type === 'script') {
if (node.id !== currentScriptKey.value) {
return;
} else {
currentScriptKey.value = '';
getTree().setSelectedKeys([]);
}
} else {
if (!keys?.includes(currentScriptKey.value)) {
return;
} else if (keys?.includes(node.id)) {
currentScriptKey.value = '';
}
}
```
简化后的代码主要是将原本分散在多个 if 语句中的逻辑整合在一起,同时简化了一些不必要的判断条件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)