Angular - How can I activate AUX route with lazy loaded module?

天大地大妈咪最大 提交于 2019-12-06 02:44:48

问题


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 :



来源:https://stackoverflow.com/questions/47857150/angular-how-can-i-activate-aux-route-with-lazy-loaded-module

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