Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 212
Royi Namir Avatar asked Dec 17 '17 16:12

Royi Namir


People also ask

How do you use a lazy load module without routing?

define where we want to load our component in the template with the ng-template tag, define its view query through ViewChild decorator, which gives us access to the DOM and defines the container to which the component will be added, finally, dynamic import the component and add it to the container.

How do I enable Angular routing?

Add the AppRoutingModule link The router is dedicated to routing and imported by the root AppModule . By convention, the module class name is AppRoutingModule and it belongs in the app-routing.module.ts in the src/app directory. Run ng generate to create the application routing module.

What is the correct way to add a basic route in Angular?

First, add links to the two components. Assign the anchor tag that you want to add the route to the routerLink attribute. Set the value of the attribute to the component to show when a user clicks on each link. Next, update your component template to include <router-outlet> .

What is lazy routing in Angular?

Lazy loading is an approach to limit the modules that are loaded to the ones that the user currently needs. This can improve your application's performance and reduce the initial bundle size. By default, Angular uses eager loading to load modules.


1 Answers

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 :

like image 59
Royi Namir Avatar answered Sep 20 '22 11:09

Royi Namir