使用路由器 "redirectTo" 中的函数来确定重定向到哪里可以防止 aot

Using a function in the router's "redirectTo" to determine where to redirect prevents aot

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

{   path : '', redirectTo: redirector(),     pathMatch: 'full' }

redirector() returns 一条路线(字符串)例如:'home/browse'

这会导致 ng build --prod --aot

出现以下问题
Uncaught Error: Invalid configuration of route ''. One of the 
following must be provided: component, redirectTo, children or 
loadChildren

Here you can see a github issue that also is related to angular guard (conditional) redirection.

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

然后在 RedirectGuardcanActivate 方法中,您可以使用:this.router.navigate(),其中 this.router 是来自 [=16= 的 Router ]: https://angular.io/guide/router,为了执行重定向。

Example use of this.router.navigate():
this.router.navigate(['/heroes']);

您可以在 this Whosebug question 中找到更多详细信息。

祝你好运!