返回

Element UI 中移除 Tab 页时的错误处理**

前端

问题

在使用 Element UI 的 Tab 组件时,当尝试通过 remove() 方法移除一个 Tab 页时,可能会遇到如下错误:

Avoided redundant navigation to current location: ***

原因

此错误的原因在于 Element UI 的 Tab 组件默认情况下会将 Tab 页的路由路径作为移除操作的目标。当您尝试移除当前激活的 Tab 页时,由于目标路径与当前路径相同,会触发此错误。

解决方案

要解决此错误,可以修改 remove() 方法中的逻辑,显式设置目标路径为 null。具体步骤如下:

  1. 打开 Element UI 的 Tab 组件源码(例如 node_modules/element-ui/lib/tabs.js)。
  2. 找到 remove() 方法(通常在第 350 行左右)。
  3. remove() 方法中,找到以下代码块:
if (this.currentName === name) {
  if (this.computedRemove) {
    this.setCurrentName(null)
  } else if (this.currentName === 'home') {
    this.pushRoute(this.currentRoute.fullPath)
  } else {
    this.pushRoute('home')
  }
}
  1. if (this.computedRemove) 中的代码替换为:
if (this.computedRemove || name === this.currentName) {
  this.setCurrentName(null)
}
  1. 保存更改。

这样修改后,当移除当前激活的 Tab 页时,将不再设置路由目标路径,从而避免了该错误。

避免错误的额外技巧

除了修改 remove() 方法之外,还可以通过以下技巧避免此错误:

  • 在移除 Tab 页之前,先将其激活。
  • 使用 force 选项移除 Tab 页,这将忽略冗余导航检查。
  • 显式设置路由目标路径为 null

总结

Element UI 中 "Avoided redundant navigation to current location" 错误是由移除当前激活的 Tab 页时导致的。通过修改 remove() 方法中的逻辑,可以显式设置目标路径为 null,从而解决此错误。