mevr
mevr

Reputation: 1125

Passing string as query params not working

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!

Upvotes: 0

Views: 210

Answers (1)

Nitheesh
Nitheesh

Reputation: 20016

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.

Upvotes: 1

Related Questions