简体   繁体   English

typescript angular2 template queryParams

[英]typescript angular2 template queryParams

I am trying to build an Angular 2 app with routing. 我正在尝试使用路由构建一个Angular 2应用程序。 I have a link in my template that looks like this: 我的模板中有一个链接,如下所示:

<a href="#" [routerLink]="['/Foo']" [queryParams]="{page:1+curr}">Next</a>

In my component class curr is defined as 在我的组件类中, curr被定义为

curr: number = 0

I want the linked URL to be 我想要链接的URL

/Foo?page=1 

instead it looks like the 1 and curr are being concatenated and the URL is 相反,它看起来像1curr被连接,URL是

/Foo?page10 

Any ideas? 有任何想法吗?

试试这个代码

<a href="#" [routerLink]="['/Foo']" [queryParams]="{page:'1'+curr}">Next</a>

The problem was that even though curr was declared curr: number = 0 it was later asigned to a variable returned via AJAX that happened to be a string 问题是即使curr被声明为curr: number = 0它稍后被赋予通过AJAX返回的变量,恰好是一个字符串

curr = data.current // current is a string

I expected this to be caught by the transpiler but I was not running a verstion of lint. 我预计这会被转录器捕获,但我没有运行lint。 Therefore when trying to add a string and a number the number coerced into a string and the result was concatenation. 因此,当尝试添加字符串和数字时,数字强制转换为字符串,结果是连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM