0

I am starting learning angular 6. I have a situation where i want to show page with different html without using header and footer. I have added in app.component.html file :

<app-header></app-header>

<router-outlet></router-outlet>
<app-footer></app-footer>

Now I want to load component without using root component.

Kyaw Kyaw Soe
  • 3,258
  • 1
  • 14
  • 25
Karan
  • 1,048
  • 2
  • 20
  • 38

2 Answers2

0

Well, You must create a router, here is an example:

In app.module.ts, add:

import { RouterModule, Routes } from '@angular/router';
const appRoutes: Routes = [
  { path: 'crisis-center', component: CrisisListComponent },
  { path: 'hero/:id',      component: HeroDetailComponent },
  {
    path: 'heroes',
    component: HeroListComponent,
    data: { title: 'Heroes List' }
  },
  { path: '',
    redirectTo: '/heroes',
    pathMatch: 'full'
  },
  { path: '**', component: PageNotFoundComponent }
];

@NgModule({
  imports: [
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    )
    // other imports here
  ],
  ...
})
export class AppModule { }

And in your app.component.html you have <router-outlet></router-outlet> and this is place where angular inject components through router, so remove header and footer and put them into the pages(components) you need to appear.

Carnaru Valentin
  • 1,690
  • 17
  • 27
0

I think you can use "ng-template" for this;

Firstly create a routing module like this;

const routes: Routes = [

    { 
    path: 'login',
    component: LoginComponent,
    data: { showRootComponents: false } 
    },
    { 
    path: 'home',
    component: HomeComponent,
    data: { showRootComponents: true } 
    }

]

export const AppRoutingModule = RouterModule.forRoot(routes);

And import this in to your AppModule;

@NgModule({
  declarations: [
    AppComponent,
  HomeComponent,
  LoginComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpModule,
    FormsModule,
    ReactiveFormsModule   
  ]
  bootstrap: [AppComponent]
})
export class AppModule { }

Then in your root component;

withoutRootComponents:boolean

public constructor(private activatedRoute:ActivatedRoute) {
    this.withoutRootComponents = activatedRoute.snapshot.data['showRootComponents']);
}

and html;

<ng-template [ngIf]="!withoutRootComponents">
   <app-header></app-header>
   <router-outlet></router-outlet>
   <app-footer></app-footer>
</ng-template>

<ng-template [ngIf]="withoutRootComponents">
   <router-outlet></router-outlet>
</ng-template>
Kıvanç B.
  • 152
  • 4
  • As described here this won't work for the OP as the root component won't get the data from route since it is the one that contains the router-outlet https://stackoverflow.com/a/45737376/186212 – Craig Sep 24 '20 at 10:49