ANGULARJS2 Learning Note (iv) routing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.