Angular 2’s ActivatedRoute allows you to get the details of the current route into your components. Params on the ActivatedRoute are provided as streams, so you can easily map the param you want off of the stream and display it in your template.

For example we have a HerosComonent, and inside HerosComponent, we will have multi HeroComponent:

heros.component.html:

<ul>
<li>
<a [routerLink]="'1'"
routerLinkActive="active"
[routerLinkActiveOptions]="{exact: true}">Hero 1</a>
</li>
</ul>

heros.routers.ts:

import {HerosComponent} from "./heros.component";
import {RouterModule} from "@angular/router";
import {HeroComponent} from "./hero/hero.component";
const routes = [
{path: '', component: HerosComponent},
{path: ':id', component: HeroComponent},
];
export default RouterModule.forChild(routes)

hero.component.ts:

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from "@angular/router"; @Component({
selector: 'app-hero',
templateUrl: 'hero.component.html',
styleUrls: ['hero.component.css']
})
export class HeroComponent implements OnInit { id;
constructor(private router: ActivatedRoute) {
this.id = router.params.map((p:any) => p.id);
} ngOnInit() {
} }

Github

[Angular2 Router] Use Params from Angular 2 Routes Inside of Components的更多相关文章

  1. [Angular2 Router] Style the Active Angular 2 Navigation Element with routerLinkActive

    You can easily show the user which route they are on using Angular 2’s routerLinkActive. Whenever a ...

  2. [Angular2 Router] Resolving route data in Angular 2

    From Article: RESOLVING ROUTE DATA IN ANGULAR 2 Github If you know Anuglar UI router, you must know ...

  3. [Angular2 Router] Auxiliary Routes bit by bit

    Article Github Auxiliary Routes is is little bit hard to understand. Here is demo, link You can see ...

  4. [Angular2 Router] Build Angular 2 Navigation with routerLink

    Angular 2 navigation is configured using the routerLink directive. The routerLink directive behaves ...

  5. [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable

    In this tutorial we are going to learn how to use the Angular 2 router to pass optional query parame ...

  6. [Angular2 Router] Index router

    Index router as default router. import {RouterModule} from "@angular/router"; import {NotF ...

  7. [Angular2 Router] Using snapshot in Router

    In the application, we have heros list, when click each hero, will redirect to hero detail view. Tha ...

  8. [Angular2 Router] Setup page title with Router events

    Article import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator ...

  9. vue router & query params

    vue router & query params vue router get params from url https://zzk.cnblogs.com/my/s/blogpost-p ...

随机推荐

  1. html5的一些表单属性

    IE8及以下不能很好支持这些属性 <pre>input属性: placeholder:输入框的默认值,向用户显示描述性说明文字或者提示信息 autocomplete:值为on和off..o ...

  2. xcode import<xx/xx.h> 头文件报错

    最近一直在写Android程序,有点久没用xcode,在写一个项目准备把 UI7Kit导进去,将iOS 7的界面适配到低版本的时候,出现了这么一个蛋疼的问题.稍微查了一下,新建项目的时候想先做一个li ...

  3. JavaScript的function对象

    我必须先说Java与JavaScript没有关系,不是我以前想的那个样子的(JavaScript是Java的一种超进化) 在JavaScript中,函数(function)就是对象. JavaScri ...

  4. js运动 模仿淘宝幻灯

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. js运动 多物体运动含Json 但是里面数值不一样

    <!doctype html> <html> <head> <meta charset = "utf-8"> <title&g ...

  6. Spark给我们带来了什么惊喜?

    Spark的一站式解决方案有很多的优势,具体如下.(1)打造全栈多计算范式的高效数据流水线     Spark支持复杂查询. 在简单的“map”及“reduce”操作之外,Spark还支持SQL查询. ...

  7. 简单版问卷调查系统(Asp.Net+SqlServer2008)

    1.系统主要涉及以下几个表  问卷项目表(Q_Naire) 问卷题目表(Q_Problem) 题目类型表(Q_ProblmeType) 题目选项表(Q_Options) 调查结果表(Q_Answer) ...

  8. putty

    PUTTY详解 http://www.wifi-robots.com/thread-915-1-1.html 如何使用Putty远程(SSH)管理Linux VPS http://www.vpser. ...

  9. HDU 1520Anniversary party(树型DP)

    HDU 1520   Anniversary party 题目是说有N个人参加party,每个人有一个rating值(可以理解为权值)和一个up(上司的编号),为了保证party的趣味性,每一个人不可 ...

  10. test是否被执行?

    procedure TForm2.Button1Click(Sender: TObject);  function test(value:boolean):boolean;  begin    res ...