I have a messaging project in angular 2.
Routing Conditions:
1) path: /login , component: LoginComponent
2) path: /inbox , component: InboxMessageComponent
3) path:/inbox/all , "Load InboxMessageListComponent in Right side Box Only",
4) path:/inbox/13 , "Load InboxMessageDetailComponent in Right side Box Only"
So I created two routing module named app-routing.module.ts and inbox-routing.module.ts.
app-routing.module.ts
@NgModule({
imports: [
RouterModule.forRoot([
{ path: 'login', component: LoginComponent},
{ path: 'inbox', component: InboxMessageComponent },
{ path: '', component: InboxMessageComponent },
{ path: '**', component: NotFoundComponent }
])
],
exports: [RouterModule]
})
inbox-routing.module.ts
@NgModule({
imports: [
RouterModule.forChild([
{path: '/inbox/list',component: InboxMessageListComponent},
{path: '/inbox/detail/:id',component: InboxMessageDetailComponent}
])
],
exports: [RouterModule]
})
app.component.ts
template : '<router-outlet></router-outlet>'<!--This route-outlet will load "LoginComponent/InboxComponent" depending upon the routing. -->
inbox-message.component.ts
template:`
<sidebar-component></sidebar-component>
<router-outlet></router-outlet> <!-- This will load InboxMessageListComponent or InboxMessageDetailComponent -->
`
But the problem is only one is working at a time. The second one is skipping.
How to Route this kind of project ?
Instead of “href” attribute of anchor tag, we use the “routerLink” attribute of Angular. The routerLink attribute allows us to link to a specific route of the Application.
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.
What is Wildcard Route in Angular? The Wildcard Route is basically used in Angular Application to handle the invalid URLs. Whenever the user enter some invalid URL or if you have deleted some existing URL from your application, then by default 404 page not found error page is displayed.
The first property, path, defines the URL path for the route. The second property, component, defines the component Angular should use for the corresponding path. Add your routes to your application. Now that you have defined your routes, add them to your application. First, add links to the two components.
Angular routing is the concept of navigating from one view to another view, so the user performs application tasks without reloading entire page. Now, you have basic understanding on what Angular 2 Component and Routing are. Let’s start with creating a sample application to understand it better.
In init, as the component loads, the value passed in router.navigate is assigned to a variable and the information for the selected language is displayed on the screen. To run any Angular application, open the terminal on the IDE or Git Bash command prompt. And you have successfully routed two different angular components!
The Router outlet is a placeholder that gets filled dynamically by Angular, depending on the current router state. In the previous tutorials, we've used the Router outlet to create basic routing. In this tutorial, we'll see advanced uses of the <router-outlet> component such as named, multiple outlets and auxiliary routing.
You have to declare one main path and childer paths for example:
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {AddProject} from './add-project.component';
import {ViewProject} from './view-project.component';
import {Project} from './charity-project.component';
import {ProjectList} from './charity-project-list.component';
export const routes: Routes = [
{
path: 'project', component: Project,
children: [
{ path: '', component: ProjectList },
{ path: 'add', component: AddProject },
{ path: 'view/:id', component: ViewProject },
{ path: 'edit/:id', component: AddProject }
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ProjectRoutes { }
In children path you have to declare your
3) path:/inbox/all , "Load InboxMessageListComponent in Left side Box Only",
4) path:/inbox/13 , "Load InboxMessageDetailComponent in Right side Box Only"
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