Frankie Drake
Frankie Drake

Reputation: 1388

Typescript changed property type?

I faced some weird problem. I have a class:

export class Example: {
    str: string;
    isActive: boolean;
}

Then I transfer some data of that class from one component to another via routerLink and queryParams... At the child component I'm doing this:

this.activatedRoute.params.subscribe((params: Example) => {
            console.log(params, typeof params.isActive); //outputs string!!
        });

//output: {str: "xxx", isActive: "true"}, "string"

First I sent queryParams in template as:

[routerLink]="['/my/route', row]",

then I tried to do it in a controller:

onBtnClick(row: Example) {
    console.log(row, typeof row.isActive);
    this.router.navigate(['/my/route'],{queryParams: row});
}
//output: {str: "xxx", isActive: true}, "boolean"

Why is it happen and how to fix this?

Upvotes: 0

Views: 120

Answers (1)

omeralper
omeralper

Reputation: 10114

Because the parameters you pass at

this.router.navigate(['/my/route'],{queryParams: row});

goes to query string. In any case it is gonna be string.

You have 2 options:

Don't use router params to pass your data. Instead, use a service. Thus, you will keep your original JSON data.

or

Remap the transferred data to your object type.

Upvotes: 1

Related Questions