Chamin  Thilakarathne
Chamin Thilakarathne

Reputation: 447

Can we use queryParams in ionic4?

We have used queryParams for angular projects. can we use queryParams in ionic project? is there any side effect or security issues?

Upvotes: 1

Views: 73

Answers (2)

Vicky Pal
Vicky Pal

Reputation: 99

this.router.navigate(['your-page-name-here'], params);

Upvotes: 2

Pankaj Sati
Pankaj Sati

Reputation: 2539

You can use queryParams but it is not a recommended way because you are sending values as a part of router link. This also means that you are limited to strings only and objects need to be stringified (JSON.stringify()) and parsed each time you send data.

Better option is to use Extras State:

let navigationExtras: NavigationExtras = {
      state: {
        userData: this.user
      }
    };
    this.router.navigate(['my-page'], navigationExtras);

In MyPage, get data from the State by injecting Router in constructor:

this.data = this.router.getCurrentNavigation().extras.state.userData;

Using Service: Also, you can create a service that has a getter and setter that you can use to save data in it before navigating and retrieve it after completing the navigation:

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class DataService {

  private data = [];

  constructor() { }

  setData(id, data) {
    this.data[id] = data;
  }

  getData(id) {
    return this.data[id];
  }
} 

Upvotes: 1

Related Questions