Using Angular 4's routing with separate files/modules for my features, I'm trying to make my app always show the login screen first then switch to my main view once the user logs in (all routes are guarded by authentication, except the login one).
I created a test app in Plunker, with notes within the app about the functionality I need, as opposed to what's currently happening: https://plnkr.co/edit/MGIGH4xpKZOtgCBl5lam
The problem I've had with most sample code (like this one) is that the component being routed to after login does not have any nested views. I got my app working by putting all the routes in a single file, but I'd prefer splitting up the routes per feature. However, as soon as I do that, things start getting wonky:
It's close, though -- typing in either "club" or "members" in the url will force the login page to show and when I click the login button, it will then show the correct page, nested in the main-nav view. Then I can use the main-nav links to flip back and forth between "club" and "members" and logout.
I think the problem may be with the way I'm doing the lazy loading, maybe?
const routes: Routes = [
{ path: '', component: MainNavComponent,
canActivate: [AuthGuard],
children: [
{ path: 'club', loadChildren: 'app/club.module#ClubModule'},
{ path: 'members', loadChildren: 'app/members.module#MembersModule'},
{ path: '', redirectTo: 'club', pathMatch: 'full' }
]
}
];
I followed the angular.io routing docs and these suggestions to get it working as well as it is, but I can't figure out the last bit that's messed up. I'd really appreciate any help!
We can nest routes, this means that for a given URL we can render a tree of components. We do this by using multiple router-outlet directives and configuring child routes on our route configuration object.
You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc.
➔ The routing mechanism allows us to navigate between different views(via components) and allowing us to configure it to make it more flexible while providing us with features like lazy loading to ease load times and increase performance.
There were a variety of issues which I addressed and added to the demo below.
@NgModule({
declarations: [
AppComponent,
PageNotFoundComponent
],
imports: [
BrowserModule,
AuthModule,
// ClubModule,
// MainNavModule,
// MembersModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
// club-routing.module.ts
const routes: Routes = [
{ path: '', component: ClubComponent }
];
// members-routing.module.ts
const routes: Routes = [
{
path: '',
component: MembersComponent,
canActivate: [AuthGuard],
children: [
{ path: ':id', component: MemberDetailComponent }
]
}
];
// app-routing.module.ts
const routes: Routes = [
{ path: '', loadChildren: 'app/main-nav.module#MainNavModule' },
{ path: 'login', component: LoginComponent },
{ path: '**', component: PageNotFoundComponent }
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With