带参数的Angular2路由失败
我在一些意想不到的地方遇到了各种各样的错误,我最好的猜测是这种情况的发生是因为路由的设置方式 这是我的app-routing.module.ts 然后,在CalendarComponent.ts中,我有一段代码:带参数的Angular2路由失败,angular,typescript,angular2-routing,angular-component-router,Angular,Typescript,Angular2 Routing,Angular Component Router,我在一些意想不到的地方遇到了各种各样的错误,我最好的猜测是这种情况的发生是因为路由的设置方式 这是我的app-routing.module.ts 然后,在CalendarComponent.ts中,我有一段代码: imports (...) constructor( private activatedRoute: ActivatedRoute, ){ } public ngOnInit(): void { this.activatedRoute.params.subs
imports (...)
constructor(
private activatedRoute: ActivatedRoute,
){
}
public ngOnInit(): void {
this.activatedRoute.params.subscribe((params: Params) => {
this.resolveURLParams(params);
});
}
public resolveURLParams( params ): void {
// do stuff based on params
}
不管怎样,就在半年前,一些RC@Angular2我可以通过直接在浏览器中键入其中任何一个来启动应用程序
本地主机:3000
本地主机:3000/日历或
本地主机:3000/calendar/2017-05-27
并将取得预期的效果。现在我必须从localhost:3000开始,这样应用程序会将我路由到../login->../calendar->../calendar/2017-05-27,否则我会遇到各种问题,比如无法读取属性subscribe of null或无法激活已激活的outlet
我想,路线已经更新了,我落后了。请提供相关帮助?订阅路由参数可能会有时间延迟,我建议您使用服务ActivatedRouteSnapshot使用不可见选项 注入服务ActivatedRouteSnapshot并使用
this.activatedRoute.route.snapshot.params.urlString
注意:对定义使用pathMatch:full
{ path: 'calendar', component: CalendarComponent, canActivate: [AuthGuard] , pathMatch:'full'},
由于您的路由将按照定义的顺序失败,并尝试将参数1与上面的匹配,这实际上是一个麻烦,但路由和ActivateRoute订阅的工作方式没有问题。问题是我使用的AuthGuard坏了。既然我修好了警卫,所有的问题都解决了。因此,感谢您尝试提供帮助。请实际列出您看到的错误。如果从/calendar开始,我会在promise:Error:无法激活已激活的插座中得到Error:Uncaught。如果我从/calendar/2017-05开始,我会得到错误:承诺中未捕获:TypeError:无法在resolveUrlParams functionMhm处读取null的属性“subscribe”,我无法重现您的错误类型。你能提供一个可运行的plnkr样本吗?
{ path: 'calendar', component: CalendarComponent, canActivate: [AuthGuard] , pathMatch:'full'},