Subscribing to route parameters and data in Angular 2

My form must have access to route parameters and data. They are observable, so I need to subscribe to access their values. My question is: how do I access them both together in the same subscription?

this._route.params.subscribe(
  params => {
    getData(params['id'], data['id2'] <=== i need data value too. how do i get this???)
  });

Why does angular put them in 2 separate observables?

+4
source share
2 answers

How can I access them together in one subscription?

You can use ActivatedRouteSnapshotfrom your own ActivatedRoute. ActivatedRouteSnapshothas the property paramsand queryParams, and you can get both values ​​at the same time. Note: we get only the initial value of the parameters using this technique

constructor(private _route: ActivatedRoute) {
    console.log(this._route.snapshot.params);
    console.log(this._route.snapshot.data);
}

Plunker example

, params data observable, zip , . , params data , .

this._route.params
  .zip(this._route.data)
  .subscribe((value) => {
    this.id = value[0]["id"]; // get param
    this.data = value[1]; // get data value
  });

Plunker

angular 2 ?

, , , .

+7

, ActivatedRouteSnapshot ActivatedRoute, data.

params, , params.

this.route.params.subscribe((params) => {
  console.log(params);
  console.log(this.route.snapshot.data);
});
+1

Source: https://habr.com/ru/post/1660560/


All Articles