0

I need to pass a string value along with navigation from one component to other using queryparams.

Component1:
 stringdata = "Hello";
 this.router.navigate(['component2'], { queryParams:  stringdata });

Component2:

ngOnInit() {

  this.route.queryParams.subscribe(params => {
    console.log(params)
  })
}

Console output I get in component2 is:-
{
"0": "H",
"1": "E",
"2": "L",
"3": "L",
"4": "O",
}

In browser after navigation I can see 
domain.com/child20=H&1=E&2=L&3=L&4=O, why it is not passing as string!
2
  • 1
    Maybe queryParams: { stringdata } ? Commented Aug 26, 2021 at 6:28
  • That is not a valid Query params. You may refer to HTML5 URLs and the <base href>. Commented Aug 26, 2021 at 6:53

1 Answer 1

1

Pass your string as query params like

this.router.navigate(['component2'], { queryParams:  { name: stringdata }});

And inside your component access it with

this.route.queryParams.subscribe(params => {
    console.log(params.name)
})

queryParams expect an object as argument.

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

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.