从 Angular 13 升级到 14 - 路由问题 - 错误:NG04014

Zin*_*hel 8 routes angular angular14 angular14upgrade

我收到此错误错误:NG04014:路由“检查//”的配置无效:redirectTo 和 canActivate 不能一起使用。重定向发生在激活之前,因此 canActivate 永远不会被执行。将 Angular 13 升级到 14 后。有人可以帮忙吗?

小智 12

1)回答问题

redirectTo 和 canActivate 不能一起使用。重定向发生在激活之前,因此 canActivate 永远不会被执行。

在 Angular 14 中,您不能再在同一配置中使用它们。在路由配置中选择“redirectTo”和“CanActivate” inspect/,根据您的情况选择最可靠的选项,然后就可以开始了。

2)关于NG04014错误

如果您偶然发现此错误

Error: NG04014: Invalid configuration of route '[your_route]/'. One of the following must be provided: component, loadComponent, redirectTo, children or loadChildren
Run Code Online (Sandbox Code Playgroud)

正如错误所示,Angular 14 不允许在不提供上述属性之一的情况下进行路由配置;你的路由配置可能只有 a ,CanActivate它在 Angular < 14 中工作正常。要解决此错误,请将 Children: [] 添加到你的路由配置中:

  {
    path: '[your_route]',
    canActivate: [your_redirect],
    children: []
  },
Run Code Online (Sandbox Code Playgroud)

3)您可能想要检查更多信息的资源和有用链接 https://github.com/angular/angular/issues/13373


Ame*_*mer 2

我认为这与验证路由器延迟加载配置有关,该配置已在Angular 14 Router 重大更改中列出:

现在,延迟加载的配置在加载后也会像初始路由集一样进行验证。具有无效路由配置的延迟加载模块现在将出现错误。请注意,这仅在开发模式下完成,因此此更改不会对生产产生影响。

我认为你有两个选择可以正确处理它:

  1. 通过返回UrlTree来处理守卫中的重定向:

如果任何守卫返回 UrlTree,则当前导航将被取消,新的导航将开始到从守卫返回的 UrlTree。 https://angular.io/api/router/CanActivate

  1. 将防护装置移动/应用canActivate到 中指定的路线上redirectTo,而不将其应用到同一路线上。