Let's say we have a list of contacts, click each contact, we can render a new route to get the detail.

Define the routers:

//contact-list.router.ts

import {ContactListComponent} from "./contact-list-component.component";
import {ContactDetailComponent} from "./contact-detail-component/contact-detail-component.component";
export const ContactsAppRoutes = [
{ path: '', component: ContactListComponent },
{ path: 'contacts/:id', component: ContactDetailComponent }
];

path: '', --> here empty path means use this component by default.

Bootstrap the router

To use router, we need to provider the router service.

bootstrap(AppComponent, [
provideRouter(ROUTER-OBJECT)
]); 
import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppComponent, environment } from './app/';
import {ContactsAppRoutes} from './app/contact-list-component/contact-list.routes';
import {provideRouter} from "@angular/router"; if (environment.production) {
enableProdMode();
} bootstrap(AppComponent, [
provideRouter(ContactsAppRoutes)
]);

Create outlet for rendering the router:

//app.component.html

<h1>
{{title}} Hello Router 3.0
</h1>
<router-outlet></router-outlet>

So the router will be render inside this router-outlet.

The contact list component:

import { Component, OnInit } from '@angular/core';
import {ContactDetailComponent} from "./contact-detail-component/contact-detail-component.component";
import {ROUTER_DIRECTIVES} from "@angular/router"; @Component({
moduleId: module.id,
directives: [ContactDetailComponent, ROUTER_DIRECTIVES],
selector: 'app-contact-list-component',
templateUrl: 'contact-list-component.component.html',
styleUrls: ['contact-list-component.component.css']
})
export class ContactListComponent implements OnInit { contacts: Array<Object>;
constructor() {
this.contacts = [
{
id: 0,
name: "Zhentian",
position: "Lead developer"
},
{
id: 1,
name: "ABC",
position: "Junior developer"
},
{
id: 2,
name: "Herry",
position: "Productor Owner"
},
{
id: 3,
name: "Janne",
position: "Master"
},
{
id: 4,
name: "Jonne",
position: "Backend developer"
}
];
}
ngOnInit() {
} }

Notice that, we have inject the ROUTER_DIRECTIVE for later use.

Template for the contact list component:

<h2>Contacts</h2>
<ul>
<li *ngFor="let contact of contacts">
<a [routerLink]="['/contacts', contact.id]">{{contact.name}}</a> |
<a routerLink="/contacts/{{contact.id}}">{{contact.name}}</a>
</li>
</ul>

To navigate to other router compoent, we need to use 'routerLink', notice there is tow ways to use 'routerLink':

<a [routerLink]="['/contacts', contact.id]">{{contact.name}}</a> 

or

<a routerLink="/contacts/{{contact.id}}">{{contact.name}}</a>

For the contact detail compoent, we want to receive an 'id', which can later fetch the data from server.

To get the param which be passed in , we need to use 'ActivedRoute':

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'; @Component({
moduleId: module.id,
selector: 'app-contact-detail-component',
templateUrl: 'contact-detail-component.component.html',
styleUrls: ['contact-detail-component.component.css']
})
export class ContactDetailComponent implements OnInit { id: number;
constructor(private route: ActivatedRoute) {
this.id = this.route.snapshot.params['id'];
} ngOnInit() {
} }

Here we use 'snapshot', this means we don't care about the later router params changes, we just need to get id and that's it. It is a cheaper solution.

Another way to do it is using ObservableActivatedRoute comes with a params property which is an Observable. To access the contact id, all we have to do is to subscribe to the parameters Observablechanges.

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'; @Component({
moduleId: module.id,
selector: 'app-contact-detail-component',
templateUrl: 'contact-detail-component.component.html',
styleUrls: ['contact-detail-component.component.css']
})
export class ContactDetailComponent implements OnInit { id: number;
constructor(private route: ActivatedRoute) {
} ngOnInit() {
this.route.params
.map(params => params['id'])
.subscribe((id) => {
this.id = id;
});
} }

But in this case, use snapshot is prefect fine, since id would change later.

Original Actical: http://blog.thoughtram.io/angular/2016/06/14/routing-in-angular-2-revisited.html

Github: https://github.com/zhentian-wan/hello-angular2

最新文章

  1. bootstrap表单带验证
  2. Service Provider模式
  3. Mac OS X Yosemite安装盘U盘制作
  4. NGUI Atlas
  5. java 21 - 10 文本文件和集合之间互相存储数据
  6. 使用javascript的stack数据结构,实现进制转换
  7. POJ2817 WordStack(状压DP)
  8. 使用compile_scripts.php脚本,生成lua打包的zip,解决加密问题
  9. UI控件之 ScrollView垂直滚动控件 和 HorizontalScrollView水平滚动控件的使用
  10. 修改SSH端口为21
  11. BGP详解
  12. nginx配置文件的说明
  13. HTML基础总结&lt;文本格式&gt;
  14. div弹出登录窗口
  15. logstash 通过type判断
  16. go:挂webserver
  17. Object Detection / Human Action Recognition 项目
  18. java学习之—递归实现二分查找法
  19. PHP与JavaScript下的Cookie操作
  20. Entries missing in table T028G T-CODE: OT51 SAP 传输配置操作为用户操作 SAP网银接口

热门文章

  1. 李洪强iOS开发之-环信04_消息
  2. javascript模板引擎Mustache
  3. Visual Studio原生开发的10个调试技巧(转)
  4. linux移植简介[MS2]
  5. Oracle系列之权限
  6. WordPress Backdoor未授权访问漏洞和信息泄露漏洞
  7. DataGrid能否动态合并一笔订单下面的多个交易
  8. 【转】ubuntu 12.04英文版设置成中文版
  9. 【转】Ubuntu安装基础教程
  10. 快速定位隐蔽的sql性能问题及调优【转载】