0

I'm trying to handle routing with nested modules in Angular in best way.

Let's say that I start application with URL localhost:5000. I should see HomeComponent with some content on left side and on the right side I have <router-outlet> where should be rendered ContactComponent. If I will go to localhost:5000/info then ContactComponent should be replaced by InfoComponent. How Can I achive this?

This is what I have so far: https://stackblitz.com/edit/angular6-material-components-demo-jb9y3u?file=src/app/home/home-routing.module.ts

(ContactComponent is not rendering and if I go to localhost:5000/info then InfoComponent will render in <router-outlet> from AppComponent

And image for better explanation:

enter image description here

3 Answers 3

3

Try this in app-routing.module

const routes: Routes = [
  {
    path: "",
    component: HomeComponent,
    children: [
      {
        path: "",
        component: ContactComponent,
      },
      {
        path: "info",
        component: InfoComponent,
      },
    ]
  },
];
Sign up to request clarification or add additional context in comments.

1 Comment

This works. The empty top route for the HomeComponent has no purpose though as this will always match.
2

You had some problems with your routes. You should learn more about using the children's property of route.

Also, pay attention to the pathMatch

link: https://stackblitz.com/edit/angular6-material-components-demo-rfeaup

Answering the comment, yes I was added lazy-loading. Otherwise, I can't see what is the sense to use the featured module. The lazy module is a functional module that separates a part of application functionality. If you are using lazy-modules, it's somewhat better for the client because the client shouldn't wait for all the app loaded, but the core.

More about lazy-loading in the Angular application: https://angular.io/guide/lazy-loading-ngmodules

Well, and about "Children" - "contains all the child routes activated under the current route" - according to the official documentation.

I believe you can find it helpful as well: https://angular.io/guide/router#child-route-configuration

5 Comments

Nice, this is almost a good answer. But perhaps, you want to explain that you've added lazy loading (and what is it), and also briefly explain how children property works or perhaps a link to the official docs? Welcome to SO, anyway! =)
If I would not to use lazy loading how the AppModule would looks like? instead loadChildren: './home/home.module#HomeModule',
you'd import the HomeModule into AppModule. And append the following in the app-routing, under HomeComponent path: children: [ { path: '', component: ContactComponent } ] Rewrite from loadChildren to 'path: '', component: HomeComponent'
So actually as @Andreas Högbom answered. Right?
Yeap, but don't forget to import HomeModule into AppModule.
0

You now have no purpose for the top level router-outlet (in AppComponent). If it would have a purpose (to match the HomeComponent on say '/home') then you could have matched a second route in the second router-outlet with some children of the 'home' route (ContactComponent => '/home/contact' and InfoComponent => '/home/info').

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.