Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 TypeError: outlet is null / cannot read property 'component' of null while routing

With Angular 5, I am trying to route to a projects page with an parameter id. After being on this page: /project/:projectid and routing in the header again to this page, only with an different ID, it throws:

columnNumber: 27683 fileName: "//kleinprodesign.new/assets/js/predependencies/zone.min.js" lineNumber: 1 message: "Uncaught (in promise): TypeError: outlet is null\nPreActivation.prototype.setupRouteGuards@//kleinprodesign.new/angular2-app/node_modules/@angular/router/bundles/router.umd.js:3601:17\nPreActivation.prototype.setupChildRouteGuards/<@//kleinprodesign.new/angular2-app/node_modules/@angular/router/bundles/router.umd.js:3550:13\nPreActivation.prototype.setupChildRouteGuards@//kleinprodesign.new/angular2-app/node_modules/@angular/router/bundles/router.umd.js:3549:9\nPreActivation.prototype.initialize@//kleinprodesign.new/angular2-app/node_modules/@angular/router/bundles/router.umd.js:3483:9\nRouter.prototype.runNavigate/

eval" lineNumber: 3601 message: "outlet is null"

The above exception is in firefox. In chrome it is: TypeError: Cannot read property 'component' of null

route tracing information

App-routing.module.ts

const routes: Routes = [
    { path: 'main', component: MainViewComponent },
    { path: 'projects', component: ProjectsOverviewComponent },
    { path: 'project/:projectid', component: ProjectItemComponent },
    { path: 'contact', component: MainViewComponent },
    { path: '', redirectTo: '/main', pathMatch: 'full' },
    { path: '**', component: MainViewComponent },
];

@NgModule({
    imports: [ RouterModule.forRoot(routes, { enableTracing: true, useHash: true })],
    exports: [RouterModule]
})

Menu.subitem.component.ts code:

OnSubItemClick(): boolean {
    this.router.navigateByUrl(this.menuSubItem.url); //this contains: /project/:id, so /project/3
}

App.component.html:

<section class="site-wrapper">
    <header-comp></header-comp>

    <section class="site-canvas">
        <section *ngIf="isMenuOpen" class="site-menu" [@isMenuOpenChanged]>
            <menu-comp></menu-comp> <!-- //this part activates the routing in the menu, via an service it shos and hides the menu via isMenuOpen  -->
        </section>
        <section *ngIf="!isMenuOpen" [@isContentChanged] class="content main-content">
            <router-outlet></router-outlet>
            <footer-comp></footer-comp>
        </section>
    </section>
</section>

So when I am on the main page and navigate to ProjectItemComponent with a random project, it all goes well. When I am in ProjectItemComponent and navigate to another project, with another id, it throws: outlet is null.

I created an plunkr to reproduce the error.

Any idea's?

like image 538
BramscoChill Avatar asked Dec 03 '17 08:12

BramscoChill


1 Answers

Finally found the solution: it appears to be an bug in angular router

I tested it by changing inside:

node_modules\@angular\router\bundles\router.umd.js node_modules\@angular\router\esm2015\router.js node_modules\@angular\router\esm5\router.js

This line:

this.canDeactivateChecks.push(new CanDeactivate(outlet.component, curr));

Into this:

if (outlet && outlet.component) {
    this.canDeactivateChecks.push(new CanDeactivate(outlet.component, curr));
}

And now its working fine.

like image 89
BramscoChill Avatar answered Oct 14 '22 20:10

BramscoChill