Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

404
Views
Why is Angular loading my root template twice

My root component is loading twice even though I have a separate component for my homepage. I have tried to use different things, such as pathMatch, separate component, etc, but no luck. When I go to /dashboard, it works just fine, but when it loads the page initially (root page), the navbar is loaded twice. I want to avoid that.+

My code looks like this:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { NavbarComponent } from './navbar/navbar.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { RouterModule, Routes } from '@angular/router';
import { RootComponent } from './root/root.component';
import { FeatureDashboardComponent } from '@angular/dashboard';
import { FeatureHomepageComponent } from '@angular/homepage';

const routes: Routes = [
  {
    path: '',
    component: RootComponent,
    pathMatch: 'full',
    children: [
      { 
        path: '',
        component: FeatureHomepageComponent
      }
    ]
  },
  {
    path: 'dashboard',
    component: FeatureDashboardComponent
  },
  {
    path: '**',
    redirectTo: ''
  }
];

@NgModule({
  declarations: [
    NavbarComponent,
    RootComponent
  ],
  imports: [
    BrowserModule,
    NgbModule,
    RouterModule.forRoot(routes)],
  providers: [],
  bootstrap: [RootComponent],
})
export class AppModule {}

And my HTML for RootComponent is:

<angular-navbar></angular-navbar>
<div class="layout">
    <router-outlet></router-outlet>
</div>

And navbar is:

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container-fluid">
      <a class="navbar-brand">My App</a>
    </div> 
</nav>

Example in inspect

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I think the problem might be that you have the route '' loading the RootComponent and the children route tries to load FeatureHomepageComponent but is also '', which can't be effectively resolved by the router.

Try giving the children route a path that is not empty, like this:

...
{
    path: '',
    component: RootComponent,
    pathMatch: 'full',
    children: [
      { 
        path: 'home',
        component: FeatureHomepageComponent
      }
    ]
  },
...

And play with the redirect at the end, so when your application loads, it should navigate to (for example) localhost:4200/home

about 4 years ago · Juan Pablo Isaza Report

0

I think the problem is with this code:

path: '',
component: RootComponent,

you most likely do NOT want to have your RootComponent here as it would allow the RootComponent to appear within your

<router-outlet> 

and could explain why you have 2 of them.

<router-outlet> 

should only route to CHILDREN of your RootComponent.

In other words, no route (in your routing config) should render your RootComponent it's already being rendered. The routes there should only render what you want to display WITHIN your router-outlet, aka CHILDREN of your RootComponent

For the solution I think you just have to make this:

path: '',
component: FeatureHomepageComponent

the first path in your config (delete the RootComponet part and the children:[] part)

about 4 years ago · Juan Pablo Isaza Report

0

Instead of calling the router-outlet try to call to the exact component name

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!