router outlet is not rendering

Viewed 5312

my project folder structure like below

|-- app
     |-- home
       |-- home-page
           |--home-page.component.css
           |--home-page.component.html
           |--home-page.component.spec.ts
           |--home-page.component.ts
       |-- login
           |--home-page.component.css
           |--home-page.component.html
           |--home-page.component.spec.ts
           |--home-page.component.ts
       |-- user-profile
           |--user-profile.component.css
           |--user-profile.component.html
           |--user-profile.component.spec.ts
           |--user-profile.component.ts
     |-- home-routing.module.ts
     |-- home.module.spec.ts
     |-- home.module.ts
|-- app-routing.module.ts
|-- app.component.css
|-- app.component.html
|-- app.component.ts
|-- app.module.ts

my home-routing.module.ts consisting like below

export const HomeRoutingModule: Routes = [
    {
        path: "user-profile",
        children: [
            {
                path: "user-profile",
                component:UserProfileComponent ,
                data: {
                    title: "user-profile",
                    urls: [{ title: "user-profile", url: "user-profile" }]
                }
            }
        ]
    }
];

and app-routing.module.ts is like below

  { path: "home", component: LoginComponent },
  {
    path: "user-profile",
    component: HomePageComponent,
    children: [
      { path: "user-profile", loadChildren: "./home/home.module#HomeModule" },
    ]
  }

now I'm trying to render user-profile.components.html inside the home-page.component.htmlfor that i tried to add the sample code snippet in the home-page.component.ts like below

<p>this is header</p>
<router-outlet></router-outlet>
<p>this is footer</p>

my url is like below localhost:4200/#/user-profile, but it is rendering only below output except content

this is header

this is footer

I think something I missed, Even I'm not able to figure out that . Could any one point out, whats went wrong in this code .

Edit: home.module.ts

import { NgModule } from '@angular/core';
import { CommonModule, } from '@angular/common';
import { RouterModule } from '@angular/router';

import { HomeRoutingModule } from './home-routing.module';
import { LoginComponent } from './login/login.component';
import { UserProfileComponent } from './user-profile/user-profile.component';
import { HomePageComponent } from './home-page/home-page.component';


@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(HomeRoutingModule),
    HomeRoutingModule,
  ],
  declarations: [UserProfileComponent, HomePageComponent]
})
export class HomeModule { }
3 Answers

The issue is with deep nesting of your path /user-profile/user-profile. You should use the path only once.

Modified version of your routing

app-routing.module.ts

{ path: "home", component: LoginComponent },
{
    path: "user-profile",
    component: HomePageComponent,
    children: [
      { path: "", loadChildren: "./home/home.module#HomeModule" },
    ]
  }

home-routing.module.ts

export const HomeRoutingModule: Routes = [
    {

                path: "",
                component:UserProfileComponent ,
                data: {
                    title: "user-profile",
                    urls: [{ title: "user-profile", url: "user-profile" }]
                }
    }
];

Edit

Remove HomeRoutingModule from imports.

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(HomeRoutingModule)
  ],
  declarations: [UserProfileComponent, HomePageComponent]
})
export class HomeModule { }

Just Change your home-routing.module.ts as :

    export const HomeRoutingModule: Routes = [
{path : "" , component : HomePageComponent},
        {
                    path: "user-profile",
                    component:UserProfileComponent ,
                    data: {
                        title: "user-profile",
                        urls: [{ title: "user-profile", url: "user-profile" }]
                    }
                }
    ];

and app-routing.module.ts as :

  { path: "home", component: LoginComponent },
  { path: "", loadChildren: "./home/home.module#HomeModule"} ,

and your router should work fine i.e / will load HomeComponent and user-profile will load UserProfileComponent.

EDIT

There is an error in your home-routing.module,ts :

import { NgModule } from '@angular/core';
import { CommonModule, } from '@angular/common';
import { RouterModule } from '@angular/router';

import { HomeRoutingModule } from './home-routing.module';
import { LoginComponent } from './login/login.component';
import { UserProfileComponent } from './user-profile/user-profile.component';
import { HomePageComponent } from './home-page/home-page.component';


@NgModule({
  imports: [
    CommonModule,
    HomeRoutingModule,
  ],
  declarations: [UserProfileComponent, HomePageComponent]
})
export class HomeModule { }

You remove RouterModule.forChild(HomeRoutingModule)from imports

app-routing.module.ts

    { path: "home", component: LoginComponent },
  {
    path: "user-profile",
    component: HomePageComponent,
    loadChildren: "./home/home.module#HomeModule"
  }

home-routing.module.ts

export const HomeRoutingModule: Routes = [
    {

                path: "",
                component:UserProfileComponent ,
                data: {
                    title: "user-profile",
                    urls: [{ title: "user-profile", url: "user-profile" }]
                }
    }
];
Related