博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular2 Router] Use Params from Angular 2 Routes Inside of Components
阅读量:4965 次
发布时间:2019-06-12

本文共 1152 字,大约阅读时间需要 3 分钟。

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:

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() {  }}

 

转载于:https://www.cnblogs.com/Answer1215/p/5907782.html

你可能感兴趣的文章
130242014034-林伟领-实验一
查看>>
Insert excel data into DB
查看>>
复制和输入-编程中
查看>>
SQLSERVER 处理两个日期相减
查看>>
区间+状压 [Haoi2016]字符合并
查看>>
ubuntu重新加载nginx配置文件
查看>>
Forbidden You don't have permission to access / on this server.
查看>>
Windows server 2008 R2中安装MySQL !
查看>>
Intellij Idea新建web项目(转)
查看>>
raspberry 安装apache2,使其支持ssl ,并创建自签名证书
查看>>
Trie树:应用于统计和排序
查看>>
C语言结构体和函数
查看>>
PHP 删除目录及目录下文件
查看>>
[BZOJ3449] [Usaco2014 Feb]Secret Code
查看>>
XHTML与HTML区别
查看>>
软考-程序设计语言基础(编译原理)
查看>>
2016峰会:项目管理与高级项目管理(广州站)
查看>>
用JAVA编写浏览器内核之实现javascript的document对象与内置方法
查看>>
linux 命令之top
查看>>
有关远程设置的问题
查看>>