Home > database >  Get parameters passed by url in ionic 5
Get parameters passed by url in ionic 5

Time:05-16

I have passed parameters in URL to my ionic application

http://localhost:8100/welcompage/overview?brand=bmw

I am using ActivatedRoute to retreive the data passed as parameters in the URL

   constructor(private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.route.params.subscribe(params => {
             console.log(params['brand']);
        });
    }

the output is always "undefined" meanwhile there are parameters in the URL

CodePudding user response:

First method:

  let brand = this.route.snapshot.paramMap.get('brand');

Second method:

this.route.paramMap.subscribe(

  (data) => {

    console.log(data.brand)

  }

);

method1 or method2 fit your needs when You already define brand params on your route url/:brand. but if You use query params url?brand=value1&property2=vaule2... You could get query params data using method3:

method3:

this.route.queryParams
  .subscribe(params => {
    console.log(params); // { brand: "bmw" }
    let brand = params.brand;
    console.log(brand); // bmw
  }
);
  • Related