1

I am sending data through routing like this

    navigateWithState(x) {
        console.log(x);
        this.router.navigateByUrl('/full-layout/add-form/1', { queryParams: {x} });
    }

And fetching it like this

 constructor(private route:ActivatedRoutet) {

      if(this.options == 1){
      console.log('edit');
      this.route
      .queryParams
      .subscribe(params => {
        console.log(params);
      });
    }
    else{
      console.log('add');

    }
}

but don't know why it is showing an empty array

You can see in the console

enter image description here

In console value, x has data that is showing in the console but when I pass through router it's showing blank.

I also try with navigationExtras like this

navigateWithState(x) {
    console.log(x);
    const queryParams: any = {};

    queryParams.myArray = JSON.stringify(x);

    const navigationExtras: NavigationExtras = {
      queryParams
    };
    this.router.navigateByUrl('/full-layout/add-form/1', navigationExtras);

}

in .ts

if(this.options == 1){
  console.log('edit');
  const myArray = this.route.snapshot.queryParamMap.get('myArray');
  console.log(myArray);

}
else{
  console.log('add');

}

It's showing null in myArray.

2 Answers 2

1

When you are using navigateByUrl, you need to add them in the URL. If you are using navigate then you can pass params.

  navigateWithState(x) {
        console.log(x);
        this.router.navigateByUrl(`/full-layout/add-form/1?x=${JSON.stringify(x)}`);
    }

With navigate you can use this. In this you will get values from the params.

  navigateWithState(x) {
        console.log(x);
        this.router.navigate(['/full-layout/add-form/1'], { queryParams: {x: JSON.stringify(x)}}); // when you get it parse it to JSON i.e JSON.parse('values');

}

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using navigateByUrl, you have to pass params like below;

this.router.navigateByUrl(`/full-layout/add-form/1?x=${x}`);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.