I am building an angular 6 application where I have two separate screens:
- Login (login, register, forgot password, profile)
- those pages are not part of the layout
- Layout (dashboard, products, invoices)
- those pages should share the same layout
My application is based on modules, so I have
- app.module.ts
- account.module.ts
- dashboard.module.ts
- products.module.ts
- invoices.module.ts
Each module has empty component containing just an outlet, except the layout(containing header, body and footer).
I want to achieve those routes:
site.com/account/login
site.com
| will navigate to the dashboardsite.com/dashboard
site.com/products
site.com/invoices
I have no problem with adding the account.module
, but I don't know how to configure the routes when adding the layout.module
Note: maybe my whole approach is wrong, but to me it is the only logical thing to do, since I have modules containing components, and I want to be prepared for possible lazy loading configuration.
Please advice me If I'm on the wrong path.
Code
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { LayoutModule } from './layout/layout.module';
import { AccountModule } from './feature-components/membership/account.module';
@NgModule({
declarations: [
AppComponent,
FetchDataComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
BrowserAnimationsModule,
HttpClientModule,
LayoutModule,
AccountModule,
BrowserAnimationsModule
RouterModule.forRoot([
{ path: '', component: LayoutOutletComponent },
{ path: '**', component: PageNotFoundComponent }
]),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.html
<router-outlet></router-outlet>
account.module.ts
import { NgModule } from '@angular/core';
import { SharedModule } from '@app-shared/shared.module';
import { LoginComponent } from './login/login.component';
import { ResetPasswordComponent } from './reset-password/reset-password.component';
import { ChangePasswordComponent } from './change-password/change-password.component';
import { RouterModule } from '@angular/router';
import { AccountOutletComponent } from './account-outlet/account-outlet.component';
@NgModule({
imports: [
SharedModule,
RouterModule.forChild(
[{
path: 'account', component: AccountOutletComponent,
children: [
{ path: 'login', component: LoginComponent },
{ path: 'reset-password', component: ResetPasswordComponent },
{ path: 'change-password', component: ChangePasswordComponent },
{ path: '', redirectTo: 'login', pathMatch: 'full' }
]
}]
)
],
exports: [
LoginComponent,
ResetPasswordComponent,
ChangePasswordComponent
],
declarations: [
AccountOutletComponent,
LoginComponent,
ResetPasswordComponent,
ChangePasswordComponent
]
})
export class AccountModule { }
So, no problems here, the problems starts when I add that layout module
which should constants the layout and inside of it, all nested modules.. and to be honest I don't even know how to start the routing configuration
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { FooterComponent } from './footer/footer.component';
import { LayoutOutletComponent } from './layout-outlet/layout-outlet.component';
import { InvoicesModule } from '../feature-components/invoices/invoices.module';
@NgModule({
imports: [
CommonModule,
InvoicesModule,
RouterModule
)],
exports: [
NavMenuComponent,
FooterComponent
],
declarations: [
NavMenuComponent,
FooterComponent,
LayoutOutletComponent
]
})
export class LayoutModule { }