Angular - How can I activate AUX route with lazy l

2020-07-15 09:57发布

问题:

I have an angular app which is loading lazily module.

At first the app is navigating to home where it loads the module ( named module1) :

main routing :

const routes: Routes = [
    { path: "", redirectTo: "/home", pathMatch: "full" },
    { path: "home", loadChildren: "./module1/module1.module#Module1Module" }, 

];

At module1 - there's also routing table :

const routes: Routes = [
    {
        path: "", component: Home1Component,
        children: [
            { path: 'bird', outlet: 'under', component: Home2Component } 
        ]
    }
];

So since it's loading lazily , when the app starts - it goes to /home where there it loads the Home1Component

But Home1Component also has a button in it which suppose to set a value to the outlet route .

home1.component.html:

<input value="click to activate aux route"  type="button" (click)="go()"/>
<router-outlet  ></router-outlet>
<router-outlet name='under'></router-outlet>  //<---- I want to activate  only this

This is how I try to activate the outlet route :

  public go() {
    this.router.navigate([{ outlets: { under: ['bird'] } }], { relativeTo: this.route })
  }

But I get an error :

Error: Cannot match any routes. URL Segment: 'home'

Question :

Why am I getting this error and how can I fix it ?

Online code here : Stackblitz

回答1:

Well with the help of @jmw5598 who referenced me to the problem/bug with the router .

There is a problem with default empty location for lazy loaded modules.

Here is a working solution for the problem .

The key thing to understand is that lazy modules should not have empty root path

Related specific description :