In this tutorial we are going to learn how to use the Angular 2 router to pass optional query parameters from one route into another route. There are couple of ways of doing this from the source route perspective: we use the queryParams property in the navigate API call, or we can use the queryParams directive.

On the receiving side, and especially in the case of detail child routes where we want to navigate from one detail into the other, we are going to see how to use the queryParams observable to receive the navigation query parameters.

First way, using in html:

    <a [routerLink]="hero.id"
routerLinkActive="active"
[queryParams]="{description: 'Starwar Hero '}"
[routerLinkActiveOptions]="{exact: true}">{{hero.name}}</a>

Second way, using in JS:

  getHeroByIndex(i){
// this.router.navigateByUrl(`/heros/${i}`);
// this.router.navigate(['heros', i]);
this.router.navigate([i], {
relativeTo: this.route,
queryParams: {
description: 'Star war Hero'
}
})
}

Read the query param:

export class HeroComponent implements OnInit {

  hero: Observable<any>;
description: string; constructor(private router: ActivatedRoute,
private starwarService: StarWarsService) { } ngOnInit() {
/* this.hero = this.router.params
.map((p:any) => p.id)
.switchMap( id => this.starwarService.getPersonDetail(id));
*/ // since herocomponent get init everytime, it would be better to use snapshot for proferemence
const heroId = this.router.snapshot.params['id'];
this.hero = this.starwarService.getPersonDetail(heroId); this.router.queryParams.subscribe(
param => this.description = param['description']
)
} }

In url, it looks like:

http://localhost:4200/heros/4?description=Starwar%20Hero%20

Github

[Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable的更多相关文章

  1. [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 ...

  2. [Angular2 Router] CanActivate Route Guard - An Example of An Asynchronous Route Guard

    In this tutorial we are going to learn how we can to configure an can activate route guard in the An ...

  3. [Angular2 Router] CanDeactivate Route Guard - How To Confirm If The User Wants To Exit A Route

    In this tutorial we are going to learn how we can to configure an exit guard in the Angular 2 Router ...

  4. $router和$route的区别,路由跳转方式name 、 path 和传参方式params 、query的区别

    一.$router和$route的区别 $router : 是路由操作对象,只写对象$route : 路由信息对象,只读对象 例子://$router操作 路由跳转 this.$router.push ...

  5. [Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation

    In this tutorial we are going to learn how to navigate programmatically (or imperatively) by using t ...

  6. Vue中美元$符号的意思与vue2.0中的$router 和 $route的区别

    vue的实例属性和方法 除了数据属性,Vue 实例还暴露了一些有用的实例属性与方法.它们都有前缀 $,以便与用户定义的属性区分开来.例如: var data = { a: 1 } var vm = n ...

  7. 浅谈vue$router 和 $route的区别

    最近在学习vue的单页面应用开发,需要vue全家桶,其中用到了VueRouter,在路由的设置和跳转中遇到了两个对象$router 和 $route ,有些傻傻分不清,后来自己结合网上的博客和自己本地 ...

  8. vue中的$router 和 $route的区别

    最近在学习vue的单页面应用开发,需要vue全家桶,其中用到了VueRouter,在路由的设置和跳转中遇到了两个对象$router 和 $route ,有些傻傻分不清,后来自己结合网上的博客和自己本地 ...

  9. 初识$router和$route

    初识\(router和\)route 一.前言 ​ vue框架中单页面富应用可以说是其最大的优点功能之一了,应用起来简单直观,说起单页面富应用那就必须得联想到\(router**,但是在项目开发过程中 ...

随机推荐

  1. 拜托,这才是“Uber”的正确读法

    在美国,私家车主可以注册成为Uber司机,这对传统的出租车行业形成了很大的挑战,同时也让Uber始终处于舆论的风口浪尖. 7月14日,美国用车应用Uber正式宣布进入北京市场.在进入中国后,Uber选 ...

  2. apache 2.4 针对某个文件限速

    http://httpd.apache.org/docs/2.4/mod/mod_ratelimit.html <Location "/downloads/*.mp4"> ...

  3. LeetCode(7) - Reverse Integer

    题目的要求就是要反转一个Integer,例如输入123,则输出321,这一题比较tricky的地方就是它有可能越界,就是说1234567899,反过来是9987654321是一个越界的Integer, ...

  4. 淘宝的ruby镜像已无人维护,使用ruby-china的RubyGems镜像

    淘宝的镜像已经无人维护了,参考 https://ruby-china.org/topics/29250 https://gems.ruby-china.org/ 使用新的镜像 $ gem source ...

  5. schedule和scheduleUpdate

    在init()函数里面加上scheduleUpdate(),这样才会每一帧都调用update(). Schedule() 函数有两种方式,一种带时间参数,一种不带时间参数. 带时间参数的,间隔指定时间 ...

  6. rhel及相关linux系统版本知识

    Rhel 此处Rhel非等同redhat哦,RedHat是红帽公司在1994年左右开发维护的linux桌面版本,在2004年左右红帽公司放弃redhat开始进军linux服务器版本开发,具体见下截图 ...

  7. T-SQL 变量

    T-SQL变量 变量的种类: 在T-SQL中,变量按生存范围可以分为全局变量(Global Variable)和局部变量(Local Variable) 1.全局变量是由系统定义的,在整个SQL Se ...

  8. 第一百九十四天 how can I坚持

    该挺妈妈话的,不该买可乐,该熬点粥喝,肚子疼,救我. 好像每天都一样,每天都在重复.. 哎.. 对了,买了点花种子,铜钱草,牡丹.玫瑰.还买了棵小多肉. 还有,老妈把咸菜给邮过来了,有点期待啊. 连续 ...

  9. 【OpenOffice+swftools】在线预览环境的搭建和xpdf中文包的配置

    [环境参数] Host:Win7 64bit VMware:VMware Workstation11.1.0 Client OS:CentOS release 6.5 (Final) 2.6.32-4 ...

  10. MYSQL数据库性能调优之六:备份

    增量备份