最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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: "", ponent: Home1Component,
        children: [
            { path: 'bird', outlet: 'under', ponent: 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 .

home1ponent.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

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: "", ponent: Home1Component,
        children: [
            { path: 'bird', outlet: 'under', ponent: 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.ponent.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

Share Improve this question edited Feb 7, 2018 at 10:28 Royi Namir asked Dec 17, 2017 at 16:30 Royi NamirRoyi Namir 149k144 gold badges492 silver badges829 bronze badges 3
  • I believe this is a bug with angular lazy loading aux route. I had a similar issue. This might help stackoverflow./questions/45394728/… – Jason White Commented Dec 17, 2017 at 18:02
  • @jmw5598 I see your answer but can you please apply this to my code? Because I dont see how it's going to work – Royi Namir Commented Dec 17, 2017 at 18:25
  • I can. I'll apply it as soon as I can get in a puter. Might be a few hours. – Jason White Commented Dec 17, 2017 at 18:28
Add a ment  | 

1 Answer 1

Reset to default 9

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 :

发布评论

评论列表(0)

  1. 暂无评论