使用路由器“redirectTo”中的函数来确定重定向的位置可以防止 aot

Dav*_*lsh 6 angular2-routing angular

我正在使用一个函数来确定在加载站点时应该将用户重定向到何处。像这样:

{   path : '', redirectTo: redirector(),     pathMatch: 'full' }
Run Code Online (Sandbox Code Playgroud)

redirector() 返回一个路由(字符串)例如: 'home/browse'

这会导致以下问题 ng build --prod --aot

Uncaught Error: Invalid configuration of route ''. One of the 
following must be provided: component, redirectTo, children or 
loadChildren
Run Code Online (Sandbox Code Playgroud)

Мар*_* П. 8

在这里您可以看到一个 github 问题,该问题也与角度保护(条件)重定向相关。

github 问题的示例解决方案是(@David Alsh在前面的评论中也提到过):
{path: '', pathMatch: 'full', children: [], canActivate: [RedirectGuard]}

然后在RedirectGuardcanActivate方法中,您可以使用:this.router.navigate(),其中来自this.router:https://angular.io/guide/router 以便执行重定向。 Router'@angular/router'

使用示例this.router.navigate()
this.router.navigate(['/heroes']);

您可以在此 stackoverflow 问题中找到更多详细信息。

祝你好运!