[Angular 2] ROUTING IN ANGULAR 2 REVISITED
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 Observable, ActivatedRoute 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
[Angular 2] ROUTING IN ANGULAR 2 REVISITED的更多相关文章
- 关于Angular.js Routing 的学习笔记(实现单页应用)
最近开始学习angular.js,发现angular.js确实很方便,也很强大.在看到 AngularJS Routing and Multiple Views 这一部分的时候,有点乱.现在通过记录一 ...
- Angular学习笔记:Angular CLI
定义 Angular CLI:The Angular CLI is a command line interface tool that can create a project, add files ...
- angular enter事件,angular回车事件
angular回车键搜索,angular enter搜索 对于搜索框,用户在输入内容后的搜索习惯不是鼠标点击搜索按钮,而是直接按enter键,那我们怎么给enter键绑定事件呢,其实很简单,代码如下: ...
- Angular 2 升级到 Angular 5
Angular 2 升级到 Angular 5 ts文件最上面的import语句里不要添加 .ts 后缀 , 不然 npm start 编译会失败 . 虽然浏览器能打开项目的URL , 但是内容会丢失 ...
- Angular系列一:Angular程序架构
Angular程序架构 Angular程序架构 组件:一段带有业务逻辑和数据的Html服务:用来封装可重用的业务逻辑指令:允许你向Html元素添加自定义行为模块: 环境搭建 安装nodeJs安装好no ...
- Part 23 to 26 Routing in Angular
Part 23 AngularJS routing tutorial In general, as the application becomes complex you will have more ...
- Angular - 预加载 Angular 模块
Angular - 预加载延迟模块 在使用路由延迟加载中,我们介绍了如何使用模块来拆分应用,在访问到这个模块的时候, Angular 加载这个模块.但这需要一点时间.在用户第一次点击的时候,会有一点延 ...
- Angular企业级开发(3)-Angular MVC实现
1.MVC介绍 Model-View-Controller 在20世纪80年代为程序语言Smalltalk发明的一种软件架构.MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并 ...
- 30行代码让你理解angular依赖注入:angular 依赖注入原理
依赖注入(Dependency Injection,简称DI)是像C#,java等典型的面向对象语言框架设计原则控制反转的一种典型的一种实现方式,angular把它引入到js中,介绍angular依赖 ...
随机推荐
- eclipse+tomcat7解决项目中文乱码的一个思路
1. 在代码层面进行编码的修改操作,参考博文的方法一:http://www.cnblogs.com/longshiyVip/p/4873058.html 2. 如果项目使用了struts2等前端框架, ...
- UVA 1160 X-Plosives
题意是一次装入物品,物品由两种元素组成,当遇到即将装入的物品与已经装入的物品形成k个物品,k种元素,跳过该物品的装入.可以将每种元素看成顶点,物品看成一条边.这样问题就转化为利用并查集求环的情况. 算 ...
- RichEdit 各个版本介绍
RichEdit是开发中经常使用到的控件,其版本自1.0起,历经好几年,好几次的更新,在此引用一篇介绍RichEdit版本的博文(http://blogs.msdn.com/b/murrays/arc ...
- Fast UI Draw (Intel出品)
Fast UI Draw in a library that provides a higher performance Canvas interface. It is designed so tha ...
- BNU29368:Check the Identity(栈)
Just determine whether an algebraic expression can always simplify to zero. Input The first line con ...
- SQL Server 连接字符串和身份验证详解
SQL Server .NET Data Provider 连接字符串包含一个由一些属性名/值对组成的集合.每一个属性/值对都由分号隔开. PropertyName1=Value1; ...
- XSD标准架构-----<xsd:element> 元素详解
声明一个元素. <element abstract = Boolean : false block = (#all | List of (extension | restriction ...
- 如何计算IP地址及CIDR,子网掩码计算
如何计算IP地址及CIDR 一. IP地址概念 IP地址是一个32位的二进制数,它由网络ID和主机ID两部份组成,用来在网络中唯一的标识的一台计算机.网络ID用来标识计算机所处的网段:主 机ID用来标 ...
- Grid表格属性
<Grid> <Grid.ColumnDefinitions> <!--添加列--> <ColumnDefinition Width="/> ...
- cannot find the word template:WordToRqm.dot的解决方法
powerdesigner安装后: C:"Program Files"Sybase"PowerDesigner 12"Add-ins"Microsof ...