0

Being new in angular, I want to structure my layout into two

  1. without navigation
  2. with navigation

Question: Is it possible to have more than 1 app.component, if yes how?

app.component.html:

<app-nav></app-nav>
<section>
  <router-outlet></router-outlet>
</section>

external.component.html:

<router-outlet></router-outlet>
4
  • in which scenario you are thinking about to use two app.component.ts? Commented Feb 15, 2019 at 10:34
  • What I want to achieve is to have pages without navigateion (eg. login, registration) and pages with navigation (eg. dashboard, settings) Commented Feb 15, 2019 at 10:39
  • There is one solution to achieve this..let me answer it, but without two app.component.ts Commented Feb 15, 2019 at 10:40
  • since <router-outlet></router-outlet> is kept in app.component.html my thought is to have another without navigation Commented Feb 15, 2019 at 10:40

2 Answers 2

1

There is a solution of that without using two app.component.ts. I put the files as I used in my current project.

app.component.ts

 <app-http-loader></app-http-loader>
  <div class="app-root-style">
    <router-outlet></router-outlet>
  </div>

app.router-modules.ts

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'auth', loadChildren: './auth/auth.module#AuthModule', canLoad: [AuthGuard] },
  { path: 'home', loadChildren: './home/home.module#HomeModule', canLoad: [HomeGuard] },
  { path: '**', component: FourZeroFourComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

auth-routing.module.ts

const routes: Routes = [
  { path: '', redirectTo: 'login' },
  { path: 'login', component: LoginComponent },
  { path: 'forgotpassword', component: ForgotPasswordComponent }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AuthRoutingModule { }

home-routing.module.ts

const routes: Routes = [
  {
    path: '', component: HomeComponent, children: [
      { path: 'dashboard/:uid', component: DashboardComponent },
      { path: 'userinfo', loadChildren: '../user/user.module#UserModule', canLoad: [HomeGuard] },
     }
    ]
  },

];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class HomeRoutingModule { }

This way you achieve the whole routes

url's will be like this:

https://localhost:4200/auth/login

https://localhost:4200/home/dashboard/23

See I use Guard here to Guard the routes if user not logged in

Sign up to request clarification or add additional context in comments.

3 Comments

Cool! Where will the navigation be add to?
I mean: <app-nav></app-nav>
I think you should add this to home.component.ts
0

You can have as many router outlets as you want. Simply declare children of your route. Something like that :

const routes = [
  { path: '', component: AppComponent, children: [
    { path: 'home', component: HomeComponent, children: [...] },
    { path: 'profile': component: ProfileComponent, children: [...] },
  ]}
];
<!-- Home component template -->
<div>
  <button (click)="login()">Login</button>
  <button (click)="signup()">Sign up</button>
</div>
<router-outlet></router-outlet>

<!-- Profile component template -->
<div>
  <button routerLink="dashboard">Dashboard</button>
  <button routerLink="settings">Settings</button>
</div>
<router-outlet></router-outlet>

4 Comments

I want the login to be the first page to load, not home.
@codegrid Seriously dude ? That's an exemple ... I'm just giving the idea, you can name your components what you want.
Thanks for the example. I have already achieved that. What I want is for the login page to be the landing or entry page.
Then replace AppComponent by LoginComponent in the route definition.

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.