Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parameter from navigateByUrl?

I do navigation using:

this.router.navigateByUrl(item.url);

Where item.url has value:

orgtree/1

Route is:

{
        path: "orgtree/:orgstructure",
        component: OrganizationsComponent
      }

Inside component I do:

 ngOnInit() {
    this.route.paramMap.subscribe(params => {
      console.log(params);

    });
}

Why I get empty params?

Also I have tried this:

this.router.navigate(['/orgtree', 1]);

NO EFFECT

I got problem:

Route should be:

{
        path: "orgtree",
        component: OrganizationsComponent
      }
like image 480
OPV Avatar asked Jan 26 '26 03:01

OPV


2 Answers

You should declare your variables and then use it:

import { ActivatedRoute } from '@angular/router';

constructor(private route: ActivatedRoute) {
  this.route.paramMap.subscribe( params => {
    this.orgstructure = params.get('orgstructure');    
  });
}

UPDATE:

You should declare your parameters like that:

this.router.navigate(['/orgtree'], {queryParams: {orgstructure: 1}});

OR if you want to use navigateByUrl method:

const url = '/probarborrar?id=33';
this.router.navigateByUrl(url);

Please, see work demo at stackblitz.

like image 77
StepUp Avatar answered Jan 28 '26 19:01

StepUp


constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
       console.log(param);
    });
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
}

Try this code, make sure route is an instance of ActivatedRoute

like image 36
Nikhil Arora Avatar answered Jan 28 '26 20:01

Nikhil Arora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!