11

I am trying to Navigate to a new page on click of an icon and below is the code

  this.prj = e.data.project_number;
  this.router.navigateByUrl('/dashboard/ProjectShipment/634');

Instead of this hardcoded query parameter 000634 I have to pass a this.prj in to it. My path is like below

const appRoutes: Routes = [
  {
  path: 'dB',
  data: { title: 'Dashboard' },
  children: [
      {
          path: 'ProjectShipment/:reportProject',
          component: ProjectShipmentComponent,
          data: { title: 'Project Shipment' },
      }
trx
  • 2,077
  • 9
  • 48
  • 97
  • 1
    I think you are talking about PATH parameter, not QUERY parameter. If you are trying to pass path param, MuruGan's answer is the right one and url will look like '/dashboard/ProjectShipment/634'. But if we are talking about query parameters, then Miguel Pinto's answer is the right one and the url will look smth like '/dashboard/ProjectShipment?prj=634' – kyselm Aug 27 '20 at 08:19

4 Answers4

15

You can use 'router.navigate' instead of 'router.navigateByUrl':

this.router.navigate([URL], {queryParams: {id: this.prj}});
Miguel Pinto
  • 523
  • 2
  • 8
  • Argument of type '{ queryParams: { id: any; }; }' is not assignable to parameter of type 'NavigationBehaviorOptions'. Object literal may only specify known properties, and 'queryParams' does not exist in type 'NavigationBehaviorOptions'.ts(2345) – Tanzeel Nov 21 '22 at 17:12
7

Simply use string interpolation

this.router.navigateByUrl(`/dashboard/ProjectShipment/${this.prj}`);
MuruGan
  • 1,402
  • 2
  • 11
  • 23
4
// Set our navigation extras object
// that passes on our global query params and fragment
let navigationExtras: NavigationExtras = {
  queryParams: {...},
  state: {...}
};

// Redirect the user
this.router.navigateByUrl(redirect, navigationExtras);

NavigationExtras docs

EDIT: https://stackoverflow.com/a/44865817/5011818 this is also worth to look at

Chiffie
  • 581
  • 3
  • 18
  • 6
    It is worth to comment that your answer seems right if you read the documentation, but it is wrong: `navigateByUrl` ignores those `NavigationExtras`. Related issues requesting an update of Angular docs: https://github.com/angular/angular/issues/22668. and https://github.com/angular/angular/issues/18798 – Ignacio Lago Feb 13 '20 at 09:18
-2

this.router.navigate(['/route1'], { variable1: "Value" }); this.router.navigateByUrl('/route1', { variable1: "Value" });

Please none: if your url is urlencoded use navigateByUrl, if your url is not urlencoded use navigate