繁体   English   中英

使用路由器的“ redirectTo”中的函数来确定重定向的位置,以防止出现问题

[英]Using a function in the router's “redirectTo” to determine where to redirect prevents aot

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

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

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

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

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

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

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

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

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM