2017-10-14 84 views
0

我遇到了一些奇怪的问题。我有一个类:Typescript改变了属性类型?

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

然后,我从一个组件通过routerLinkqueryParams转让该类的一些数据到另一个......在子组件我这样做:

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

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

第一我送queryParams模板为:

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

然后我试图做一个控制器:

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

为什么会发生,以及如何解决这个问题?

+0

什么是错误? – Chandru

+0

布尔属性'isActive'是如何变成'string'的? – Daria

回答

1

因为你通过在

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

的参数去query string。无论如何,这将是弦乐。

你有2种选择:

不要使用路由器PARAMS来传递数据。相反,使用服务。因此,您将保留原始的JSON数据。

将传输的数据重新映射到您的对象类型。

+0

是的,我已经理解了。谢谢! – Daria

相关问题