【发布时间】:2017-05-27 20:49:34
【问题描述】:
我遇到了麻烦(在非常意想不到的地方出现各种错误),我最好的猜测是它的发生是因为路由设置的方式。
这是我的 app-routing.module.ts
const appRoutes: Routes = [
{ path: 'calendar', component: CalendarComponent, canActivate: [AuthGuard] },
{ path: 'calendar/:urlString', component: CalendarComponent, canActivate: [AuthGuard] },
{ path: 'myprofile', component: ProfileComponent, canActivate: [AuthGuard] },
{ path: 'profiles', component: ProfileComponent, canActivate: [AuthGuard] },
{ path: 'locations', component: LocationComponent, canActivate: [AuthGuard] },
{ path: 'login', component: AuthComponent },
{ path: '', redirectTo: '/login', pathMatch: 'full' }
];
@NgModule({
imports: [ RouterModule.forRoot(appRoutes) ],
exports: [ RouterModule ]
})
然后,在 CalendarComponent.ts 我有这段代码:
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)我可以通过直接在浏览器中输入任何这些来启动应用程序
localhost:3000,
localhost:3000/calendar 或
localhost:3000/calendar/2017-05-27
并且会得到预期的结果。现在我有从localhost:3000开始,所以应用程序引导我通过../login --> ../calendar --> ../calendar/2017-05-27,否则我得到各种麻烦,比如Cannot read property subscribe of null或Cannot activate already activated outlet。
我猜,路由已经更新,我落后了。请问这方面有什么帮助吗?
【问题讨论】:
-
实际列出您看到的错误会对您有所帮助。
-
如果我从
/calendar开始,我会得到 错误:未捕获(承诺中):错误:无法激活已激活的插座。如果我从/calendar/2017-05开始,我得到 Error: Uncaught (in promise): TypeError: Cannot read property 'subscribe' of null at resolveUrlParams function -
嗯,我无法重现您的错误。能否提供一个可运行的 plnkr 示例?
标签: angular typescript angular2-routing angular-component-router