我遇到了麻烦(在非常意想不到的地方出现了各种错误),我最好的猜测是它的发生是因为路由设置的方式。
这是我的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
or Cannot activate already activated outlet
。
我想,路由已经更新,我落后了。请问这方面有什么帮助吗?