ANGULAR2 routing is an important part of the management of ANGULAR2 application internal navigation, in angular applications, many components are combined to complete a complex application, it is inevitable that we often switch between views, it is necessary to use the route to manage the transformation between views.
Route definition
Let's look at a simple route definition
<reference path= ". /node_modules/angular2/typings/browser.d.ts "/>
Import {Component} from ' Angular2/core ';
Import {routeconfig, router_directives} from ' Angular2/router ';
Import {parentcmp} from "./parent.component";
Import {planetlist} from "./planet-list.component";
@Component ({
Selector: ' My-app ',
Template: '
<nav>
<a [routerlink]= "[' Parent ']" >parent</a>
<a [routerlink]= "[' Planetlist ']" >planetList</a>
</nav>
<router-outlet></router-outlet>
`,
directives: [Router_directives]
})
@RouteConfig ([
{
Path: '/contact ',
Name: ' Parent ',
COMPONENT:PARENTCMP,
Useasdefault:true
},
{
Path: '/planetlist ',
Name: ' Planetlist ',
Component:planetlist
}
])
Export class Routeexampleappcomponent {}
The process of routing definition can be seen from this example
1. Introduce the routing component import {routeconfig, router_directives} from ' Angular2/router '
2. Introduce the navigation components we need
Import {parentcmp} from "./parent.component";
Import {planetlist} from "./planet-list.component";
3. Define components, configure templates, define routes
<a [routerlink]= "[' Parent ']" >parent</a> defines a route where the Parent is one of our routing labels, from the following routing configuration, paying attention to the use of hump expression.
<router-outlet></router-outlet> defines where the view is displayed, that is, the module display area of the navigation
directives: [Router_directives], introducing routing instructions
@RouteConfig: Used for routing configuration where path is only routed to the URL that can be seen; name refers to the name of the route, which is consistent with the navigation above; the component that component routes to is the component that the route points to.
Such a simple routing component is basically done
Routing uses
Import {bootstrap} from ' Angular2/platform/browser ';
Import {router_providers} from ' Angular2/router ';
Import {routeexampleappcomponent} from "./myroute";
Bootstrap (Routeexampleappcomponent, [router_providers]);
The difference between this and the previous bootstrap component invocation is that the routing service needs to be injected into the angular in addition to the custom components.
ANGULARJS2 Learning Note (iv) routing