[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 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
[Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- $router和$route的区别,路由跳转方式name 、 path 和传参方式params 、query的区别
一.$router和$route的区别 $router : 是路由操作对象,只写对象$route : 路由信息对象,只读对象 例子://$router操作 路由跳转 this.$router.push ...
- [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 ...
- Vue中美元$符号的意思与vue2.0中的$router 和 $route的区别
vue的实例属性和方法 除了数据属性,Vue 实例还暴露了一些有用的实例属性与方法.它们都有前缀 $,以便与用户定义的属性区分开来.例如: var data = { a: 1 } var vm = n ...
- 浅谈vue$router 和 $route的区别
最近在学习vue的单页面应用开发,需要vue全家桶,其中用到了VueRouter,在路由的设置和跳转中遇到了两个对象$router 和 $route ,有些傻傻分不清,后来自己结合网上的博客和自己本地 ...
- vue中的$router 和 $route的区别
最近在学习vue的单页面应用开发,需要vue全家桶,其中用到了VueRouter,在路由的设置和跳转中遇到了两个对象$router 和 $route ,有些傻傻分不清,后来自己结合网上的博客和自己本地 ...
- 初识$router和$route
初识\(router和\)route 一.前言 vue框架中单页面富应用可以说是其最大的优点功能之一了,应用起来简单直观,说起单页面富应用那就必须得联想到\(router**,但是在项目开发过程中 ...
随机推荐
- Javascript 日期时间格式正则
因为Javascript的日期格式判断可能因浏览器的版本有所不同,所以用正则判断会比较好,这里备注一个正则用来判断日期时间的格式: ^(?=\d)(?:(?!(?:1582(?:\.|-|\/)10( ...
- 初识MFC,WinForm,WPF,Q't
MFC和QT是C++中常见的GUI框架,而WinForm和WPF是C#中常用的框架,不过我们一般很少叫WinForm框架,可能直接叫图形控件类库更多点.反正只是个称呼罢了,爱咋叫就咋叫.另外WinFo ...
- 从 Page not found: / 提示说起,我是怎么发现webstrom与myeclipse冲突问题及解决的
#从 Page not found: / 提示说起,我是怎么发现webstrom与myeclipse冲突问题的 ## 从前面发表了两篇博文,[webstorm+nodejs+JetBrains ID ...
- 《Java数据结构与算法》笔记-CH5-链表-3双端链表
/** * 双端链表的实现 */ class LinkA { public long dData; public LinkA next; public LinkA(long d) { dData = ...
- gradle 及 git 环境下利用hook及gradle脚本自动添加versioncode和versionname的方法
在 app/build.gradle 文件里添加几行代码: def gitCommitShortHash = 'git log -1 --pretty=%h'.execute([], project. ...
- Time vs Story Points Estimation [转]
One of the most common questions we get is whether to estimate in time or points. It seems like poin ...
- 为什么在Spring的配置里,最好不要配置xsd文件的版本号
为什么dubbo启动没有问题? 原文链接:http://www.tuicool.com/articles/YRn67zM 这篇blog源于一个疑问: 我们公司使了阿里的dubbo,但是阿里的开源网站h ...
- 【多线程】Java并发编程:并发容器之CopyOnWriteArrayList(转载)
原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容 ...
- ControlsFX8.0.2中对话框无法判断是否显示的修改
在org.controlsfx.dialog.FXDialog.java中加入 public abstract boolean isShowing(); 在org.controlsfx.dialog. ...
- html5 +css3 第一章学习和笔记
各位同学.身为本版的斑竹.,希望各位童鞋都能学到Html5 .特此没两天更新一个学习笔记和大家一起学习Html5.... 语法改变 1.新增的元素和废除的元素 2.新增的属性和 ...