记录历史 bug 修复经验,避免重复踩坑。
菜单递归查找 label 时,找不到应返回 null 而非 key,避免 truthy 值导致提前返回。
// 错误:返回 key 可能导致外层误判
function findLabel(key, items) {
for (const item of items) {
if (item.key === key) return item.label
if (item.children) {
const found = findLabel(key, item.children)
if (found) return found // 错误:key 本身是 truthy
}
}
return key // 错误
}
// 正确:找不到返回 null
function findLabel(key, items) {
for (const item of items) {
if (item.key === key) return item.label
if (item.children) {
const found = findLabel(key, item.children)
if (found) return found
}
}
return null // 正确
}
App.jsx 中的 Route path 必须与菜单 key 完全一致,包括前导斜杠:
| 类型 | 示例 |
|---|---|
| 菜单 key | /purchase/order |
| 路由 path | /purchase/order(不是 purchase/order) |
标签页关闭时需判断是否为最后一个可关闭标签,避免关闭后 activeKey 指向不存在的标签。