1

I have task - to transmit some params in App component of Angular 2 application through URL.

The best way i found to do it - using queryParams. But i not found best way to parse these params.

I'm sure that using ActivatedRoute will solve my task (and i know how to import it and how to use in constructor) but i'm not sure how to extract queryParams using it.

Can you help me in it?

Unfortunately i couldn't run my code in plunker(couldn't find properly example on Angular 2), attaching it as is.

import {Component} from '@angular/core';
import { ActivatedRoute, NavigationExtras } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Component({
  selector: 'my-app',
  styles: [],
  template: 
  `
  <button (click)="testLink()">Make link</button>
  <button (click)="checkEntity()">Check Params</button>
  `
})
export class App {
  entity: any;

  constructor(
    private route: ActivatedRoute) {
  }

  ngOnInit() {

  }

  checkEntity(){
    this.entity = this.route
      .queryParams
      .map(params => params['entity'] || 'None');

    if(this.entity) alert('It works!');
  }

  testLink(){
    let navigationExtras: NavigationExtras = {
      queryParams: { 'entity': 'user' }
    };

    this.router.navigate(['/'], navigationExtras);
  }
}

Last step - to get queryParams in entity. This code using Observable but for me will be enough single check.

8
  • Did you try a search here on SO? Did you check the docs at angular.io? Commented Sep 1, 2016 at 17:39
  • Yes, red docs on angular.io, i found close to complete solution but i can't realize the last step. I making plunker solution now to make my question more representative. Commented Sep 1, 2016 at 17:41
  • Then please post the code that demonstrates what you tried and where you're stuck and error messages if you get some. We can't know what "the last step" was for you. Commented Sep 1, 2016 at 17:42
  • 1
    Sure. I understood that i was must to attach the code with question creation. Commented Sep 1, 2016 at 17:46
  • 2
    angular.io/docs/ts/latest/guide/router.html#!#query-parameters Commented Sep 1, 2016 at 17:57

1 Answer 1

2

I am passing parameters in my ionic 4 app like this. All that is missing in the code is to:

  1. subscribe to the observable when retrieving
  2. stringify the query parameters when passing
    export class App {
      entity: any;
      constructor(private route: ActivatedRoute) { }

      checkEntity() {
        this.route.queryParams.subscribe(params => { 
          if (params) {
            if (params.entity) {
              alert('It works!'); 
            } 
          } 
        })
      }

      testLink() {
        let navigationExtras: NavigationExtras = { 
          queryParams: { 
            entity: JSON.stringify('user') 
          } 
        };
        this.router.navigate(['/'], navigationExtras);
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.