[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依赖 ...
随机推荐
- cocos-html5 Json 灵活 遍历方式 不同方式的缺陷,优点总结
1,四种解析Json的方式:Part 1 var list1 = [1,3,4]; alert(list1[1]); var list2 = [{"name":"leam ...
- bios作用
BOIS有四个作用: 一:POST 怎么讲呢? 也就是: Power On self rest ,检测主板各个设备,并sel error log. 二:计算机参数配置,也就是硬件和功能设置.例如内存啊 ...
- 函数page_cur_search_with_match
/****************************************************************//** Searches the right position fo ...
- Oracle.ManagedDataAccess.Client注意事项
OracleConnection m_DbConnection = new OracleConnection(connectionString); if (m_DbConnection.State = ...
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如 WPF中不规则窗体与WebBrowser控件的兼 ...
- Microsoft Azure File 服务简介
我们非常高兴地宣布在微软Azure中国区推出 Microsoft Azure File 服务预览版.Azure File 服务使用标准 SMB 2.1 协议提供文件共享.Azure 中运行的应用程序现 ...
- 如何监听非本地IP
做HA的时候,如果每个机器上同时需要监听多个IP的同一个端口.那么肯定是需要写死监听的IP和端口.比如在haproxy里面: frontend free bind default_backend te ...
- linux下passwd命令设置修改用户密码
1.passwd 简单说明: 我们已经学会如何添加用户了,所以我们还要学习设置或修改用户的密码:passwd命令的用法也很多,我们只选如下的几个参数加以说明:想了解更多,请参考man passwd或p ...
- ms sql server缓存清除与内存释放
Sql Server系统内 存管理在没有配置内存最大值,很多时候我们会发现运行Sql Server的系统内存往往居高不下.这是由于他对于内存使用的策略是有多少闲置的内存就占用多少,直到内存使用虑达到系 ...
- 树莓PI安装web服务器
参考:http://www.eeboard.com/bbs/forum.php?mod=viewthread&tid=27383 http://www.eeboard.com/bbs/thre ...