filmov
tv
Angular 2 routing tutorial
Показать описание
Text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
Angular 2 Tutorial playlist
Angular 2 Text articles and slides
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
Tags
angular 2 router html5 mode
angular 2 cannot find primary outlet to load component
angular2 routing hash
angular 2 router link active
angular 2 router tutorial
angular 2 router use hash
angular 2 router wildcard
angular 2 router with hash
angular 2 router youtube
angular 2 routing hash
angular 2 routing href
In this video we will discuss the basics of routing in Angular 2. Routing allows users to navigate from one view to another view.
At the moment, we have EmployeeListComponent in our application. Let's create another simple HomeComponent so we can see how to navigate from HomeComponent to EmployeeListComponent and vice-versa.
If the user tries to navigate to a route that does not exist, we want to route the user to PageNotFoundComponent. So let's create this component as well.
Here are the steps to implement routing in Angular 2 applications.
[base href="/src/"]
// Routes is an array of Route objects
// Each route maps a URL path to a component
// The 3rd route specifies the route to redirect to if the path
// is empty. In our case we are redirecting to /home
// The 4th route (**) is the wildcard route. This route is used
// if the requested URL doesn't match any other routes already defined
const appRoutes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'employees', component: EmployeeListComponent },
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
// To let the router know about the routes defined above,
// pass "appRoutes" constant to forRoot(appRoutes) method
@NgModule({
imports: [
BrowserModule, FormsModule, HttpModule,
],
declarations: [AppComponent, HomeComponent, …],
bootstrap: [AppComponent]
})
export class AppModule { }
Important: The order of the routes is very important. When matching routes, Angular router uses first-match wins strategy. So more specific routes should be placed above less specific routes. In the configuration above, routes with a static path are listed first, followed by an empty path route, that matches the default route. The wildcard route comes last because it matches every URL and should be selected only if no other routes are matched first.
We are using Bootstrap nav component to create the menu. We discussed Bootstrap nav component in Part 27 of Bootstrap tutorial.
The routerLink directive tells the router where to navigate when the user clicks the link.
The routerLinkActive directive is used to add the active bootstrap class to the HTML navigation element whose route matches the active route.
The router-outlet directive is used to specify the location where we want the routed component's view template to be displayed.
The routerLink, routerLinkActive and router-outlet directives are provided by the RouterModule which we have imported in our application root module.
If you forget to specify the router-outlet directive, you will get and error stating - cannot find primary outlet to load component.
If you do not have the above url rewrite rule, when you referesh the page you will 404 page not found error.
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
Angular 2 Tutorial playlist
Angular 2 Text articles and slides
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
Tags
angular 2 router html5 mode
angular 2 cannot find primary outlet to load component
angular2 routing hash
angular 2 router link active
angular 2 router tutorial
angular 2 router use hash
angular 2 router wildcard
angular 2 router with hash
angular 2 router youtube
angular 2 routing hash
angular 2 routing href
In this video we will discuss the basics of routing in Angular 2. Routing allows users to navigate from one view to another view.
At the moment, we have EmployeeListComponent in our application. Let's create another simple HomeComponent so we can see how to navigate from HomeComponent to EmployeeListComponent and vice-versa.
If the user tries to navigate to a route that does not exist, we want to route the user to PageNotFoundComponent. So let's create this component as well.
Here are the steps to implement routing in Angular 2 applications.
[base href="/src/"]
// Routes is an array of Route objects
// Each route maps a URL path to a component
// The 3rd route specifies the route to redirect to if the path
// is empty. In our case we are redirecting to /home
// The 4th route (**) is the wildcard route. This route is used
// if the requested URL doesn't match any other routes already defined
const appRoutes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'employees', component: EmployeeListComponent },
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
// To let the router know about the routes defined above,
// pass "appRoutes" constant to forRoot(appRoutes) method
@NgModule({
imports: [
BrowserModule, FormsModule, HttpModule,
],
declarations: [AppComponent, HomeComponent, …],
bootstrap: [AppComponent]
})
export class AppModule { }
Important: The order of the routes is very important. When matching routes, Angular router uses first-match wins strategy. So more specific routes should be placed above less specific routes. In the configuration above, routes with a static path are listed first, followed by an empty path route, that matches the default route. The wildcard route comes last because it matches every URL and should be selected only if no other routes are matched first.
We are using Bootstrap nav component to create the menu. We discussed Bootstrap nav component in Part 27 of Bootstrap tutorial.
The routerLink directive tells the router where to navigate when the user clicks the link.
The routerLinkActive directive is used to add the active bootstrap class to the HTML navigation element whose route matches the active route.
The router-outlet directive is used to specify the location where we want the routed component's view template to be displayed.
The routerLink, routerLinkActive and router-outlet directives are provided by the RouterModule which we have imported in our application root module.
If you forget to specify the router-outlet directive, you will get and error stating - cannot find primary outlet to load component.
If you do not have the above url rewrite rule, when you referesh the page you will 404 page not found error.
Комментарии