Angular2 routing issues - Zone aware error No elements in sequence

懵懂的女人 提交于 2019-12-12 04:05:25

问题


I have searched quite a bit and couldn't find any answer that solved my problem. So I am posting this question.

My issue is very similar to this question. Angular 2.0.1 Router EmptyError: no elements in sequence

But I couldn't resolve it even by adding pathMatch: 'full',.

I am getting an intermittent zonewaware error when try to navigate from a list table (navigates to detail view) below is my module.

@NgModule({
imports: [
    CommonModule,
    RouterModule.forChild([
        {
            path: 'teams',
            component: TeamsListComponent,
            pathMatch: 'full',
            resolve: {
                TeamTemplate: TeamListTemplatesResolver
            },
            canActivate: [AuthenticatedGuard]
        }, {
            path: 'teams/:id',
            component: TeamFormComponent,
            pathMatch: 'full',
            resolve: {
                team: TeamFormTeamResolver,
                resources: TeamFormResourcesResolver
            },
            canActivate: [AuthenticatedGuard]
        }
    ]),

my authGuard service has a canActivate method which just returns a boolean.

public canActivate(): boolean {
    if (this.authService.isLoggedIn()) {
        return true;
    }
    this.router.navigate(['/logout', { redirect: location.pathname }]);
    return false;
}

And here is the error: Zone aware error

I could get a router event log with {enableTracing: true}:

Router Event: NavigationStart
Router Event: RoutesRecognized
Router Event: GuardsCheckStart
Router Event: GuardsCheckEnd
Router Event: ResolveStart
Router Event: NavigationError

回答1:


Thanks for anyone who looked at this issue. I got the answer to my question.

As I described, I have few resolvers while I route to the detail page. On one of those resolvers there's a logic to get elements.

public resolve(_route: ActivatedRouteSnapshot, _state: RouterStateSnapshot): Observable<T[]> {
    return this.service.browse({}).first();
}

https://stackoverflow.com/a/42346203/5162622 - As mentioned here, the first() is sending error notification as there's no values. So I replaced it with take(1) and all looks good.

As I mentioned above in the comment, it was good to know how to do event tracking while routing. That's how I could track this down.



来源:https://stackoverflow.com/questions/45910815/angular2-routing-issues-zone-aware-error-no-elements-in-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!