[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

Github

时间: 2024-08-07 16:29:30

[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 resolve function in ui router, which you can load data before template and controller get inited. In Angular2 router, you can also use resovler. The r

[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 Angular 2 router. We are going to implement the concrete example where a user can only enter a certain route if its authorized to do so. We are also goi

[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. We are going to learn how to use a CanDeactivate route guard to ask the user if he really wants to exist the screen, giving the user to for example

[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 the Router API. We are going to learn how to use the function navigateByUrl to navigate using a manually constructed string, but we are also going to l

阿里矢量图的应用--flex布局--vue中$router和$route的方法

1.阿里矢量图字体图标的用法 2.flex布局 display:flex:设置父容器为伸缩盒子,会使每一个子元素自动变成伸缩项 接着设置子元素主轴方向上的排列方式 justify-content: flex-start让子元素从父容器的起始位置开始排列: flex-end:让子元素从父容器的结束位置开始排列: ? center:让子元素从父容器的中间位置开始排列: ? space-between:左右对齐父容器的开始和结束,中间平均分页,产生相同的间距: ? space-around:将多余的空

彻底搞懂$router 和 $route

之前,一直对$router和$route之间的区别比较模糊,后来结合网上的博客和官方给出的解释了解了他们之间的区别. 1.router是VueRouter的一个对象,通过Vue.use(VueRouter)和VueRouter构造函数得到一个router的实例对象,这个对象中是一个全局的对象,他包含了所有的路由包含了许多关键的对象和属性. 举例: 1.$router.push({path:'home'});本质是向history栈中添加一个路由并且添加一个history记录,在我们看来是 切换路

Angular2 Router

1 import Router import {RouteConfig, RouterOutlet, RouterLink, routerInjectables} from 'angular2/router'; 2 setting your RouteConfig @RouteConfig([ {path: '/', component: List, as: 'list'}, {path: '/about', component: Add, as: 'add'}, {path: '/help',

[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 each category has an own 'View detials' button, and there is a side menu on the right side. What we want is when we click the "View details" butt

Wrong number of parameters: expected 0, was given 1 Query: select count(*) from product where pname like &#39;%?%&#39; Parameters: [%小%]

eclipse在dao层写的模糊查询sql:String sql = "select count(*) from product where pname like '%?%'": 这样写会导致以下错误 Wrong number of parameters: expected 0, was given 1 Query: select count(*) from product where pname like '%?%' Parameters: [%小%]<br/> 原因: