打字稿:尝试添加两个变量但得到两个变量的串联
Typescript : Trying the addition of two variables but get the concatenation of the two
我的 Typescript 中有三个变量 class :
A:number;
B:number;
C:number;
在 class 的另一部分,我尝试添加两个变量 A 和 B :
this.C = this.A+this.B; // A =20 and B = 50;
我在 html 模板中显示 C
<span>{{C}}</span>
我的问题是,我没有得到两个变量 (20+50=70)
的加法,而是得到了串联 (2050)!!
有人可以帮我吗?
更新:
这是导致问题的确切代码部分:
goTo(page:number,type:script) {
//
this.pageFirstLineNumber = page;
this.pageLastLineNumber = page + this.LINE_OFFSET; //concatenation!!
}
注意 pageLastNumber 被声明为数字类型,LINE_OFFSET 是其他数字类型,我找到了解决这个问题的方法但是打字稿编译器输出错误(禁止评估):
////
....
this.pageFirstLineNumber = eval(page.toString()); // now It works !!
this.pageLastLineNumber = page + this.LINE_OFFSET; //concatenation!!
更新
这里是 LINE_OFFSET 变量的声明:
private _calculateOffset(fontSize: number) {
let linesDiff = (fontSize * 27) / 14;
let lines:number = 27 - (linesDiff - 27);
this.LINE_OFFSET = Math.floor(lines);
if (fontSize >= 17 && fontSize <= 20) {
this.LINE_OFFSET += (Math.floor(fontSize / 3) - 2);
}
if (fontSize > 20 && fontSize <= 23) {
this.LINE_OFFSET += (Math.floor(fontSize / 2) - 2);
}
if (fontSize > 23 && fontSize <= 25) {
this.LINE_OFFSET += (Math.floor(fontSize / 2));}
if (fontSize > 25 && fontSize <= 27) {
this.LINE_OFFSET += (Math.floor(fontSize / 2) + 1);
}
if (fontSize > 27 && fontSize <= 30) {
this.LINE_OFFSET += (Math.floor(fontSize / 2) + 4);
}
}
这意味着 A 或 B 变量中有字符串值。检查您的代码是否存在不安全部分,我的意思是转换为 <any>
,并将服务器响应转换为接口。这可能导致 number
变量中有 string
值。
问题是变量类型转换没有完成。
您需要按照以下方式进行。
A : parseInt(数字);
B : parseInt(数字);
那么你将得到总和 C= A+b 而不是串联。
当您在接口中声明 属性 是 number
时,它仅作为声明保留,不会被转换为 javascript。
例如:
interface Response {
a: number;
b: number;
}
let jsonString = '{"a":"1","b":"2"}';
let response1 = JSON.parse(jsonString) as Response;
console.log(typeof response1.a); // string
console.log(typeof response1.b); // string
console.log(response1.a + response1.b); // 12
如您所见,json 将 a
和 b
作为字符串而不是数字,在接口中将它们声明为数字对运行时结果没有影响.
如果您从服务器获得的内容被编码为字符串而不是数字,那么您需要转换它们,例如:
let response2 = {
a: Number(response1.a),
b: Number(response1.b)
} as Response;
console.log(typeof response2.a); // number
console.log(typeof response2.b); // number
console.log(response2.a + response2.b); // 3
最后我找到了导致错误的原因,我从html模板中获取了页面变量(它是一个输入值),它在函数参数中定义为数字类型,但实际上是一个字符串打字稿无法检查 html 模板中的变量类型,因此当尝试 parseInt(page) 静态打字时会突出显示错误!我通过给页面变量一个“”类型,然后将 parseInt 应用于页面变量来解决这个问题。
我运行遇到类似问题,解决如下:
C:number =0;
A:number=12;
B:number=0.4;
C= Number.parseInt(A.toString()) + Number.parseFloat(B.toString());
console.log("C=" + C );
似乎很愚蠢,将数字转换为字符串并再次解析为数字,但这就是我解决问题的方法。
在数字前加上 +:
let a = +b + +c;
常量值 = Number(stringOrNum)+1;
我的 Typescript 中有三个变量 class :
A:number;
B:number;
C:number;
在 class 的另一部分,我尝试添加两个变量 A 和 B :
this.C = this.A+this.B; // A =20 and B = 50;
我在 html 模板中显示 C
<span>{{C}}</span>
我的问题是,我没有得到两个变量 (20+50=70)
的加法,而是得到了串联 (2050)!!
有人可以帮我吗?
更新:
这是导致问题的确切代码部分:
goTo(page:number,type:script) {
//
this.pageFirstLineNumber = page;
this.pageLastLineNumber = page + this.LINE_OFFSET; //concatenation!!
}
注意 pageLastNumber 被声明为数字类型,LINE_OFFSET 是其他数字类型,我找到了解决这个问题的方法但是打字稿编译器输出错误(禁止评估):
////
....
this.pageFirstLineNumber = eval(page.toString()); // now It works !!
this.pageLastLineNumber = page + this.LINE_OFFSET; //concatenation!!
更新
这里是 LINE_OFFSET 变量的声明:
private _calculateOffset(fontSize: number) {
let linesDiff = (fontSize * 27) / 14;
let lines:number = 27 - (linesDiff - 27);
this.LINE_OFFSET = Math.floor(lines);
if (fontSize >= 17 && fontSize <= 20) {
this.LINE_OFFSET += (Math.floor(fontSize / 3) - 2);
}
if (fontSize > 20 && fontSize <= 23) {
this.LINE_OFFSET += (Math.floor(fontSize / 2) - 2);
}
if (fontSize > 23 && fontSize <= 25) {
this.LINE_OFFSET += (Math.floor(fontSize / 2));}
if (fontSize > 25 && fontSize <= 27) {
this.LINE_OFFSET += (Math.floor(fontSize / 2) + 1);
}
if (fontSize > 27 && fontSize <= 30) {
this.LINE_OFFSET += (Math.floor(fontSize / 2) + 4);
}
}
这意味着 A 或 B 变量中有字符串值。检查您的代码是否存在不安全部分,我的意思是转换为 <any>
,并将服务器响应转换为接口。这可能导致 number
变量中有 string
值。
问题是变量类型转换没有完成。 您需要按照以下方式进行。
A : parseInt(数字); B : parseInt(数字);
那么你将得到总和 C= A+b 而不是串联。
当您在接口中声明 属性 是 number
时,它仅作为声明保留,不会被转换为 javascript。
例如:
interface Response {
a: number;
b: number;
}
let jsonString = '{"a":"1","b":"2"}';
let response1 = JSON.parse(jsonString) as Response;
console.log(typeof response1.a); // string
console.log(typeof response1.b); // string
console.log(response1.a + response1.b); // 12
如您所见,json 将 a
和 b
作为字符串而不是数字,在接口中将它们声明为数字对运行时结果没有影响.
如果您从服务器获得的内容被编码为字符串而不是数字,那么您需要转换它们,例如:
let response2 = {
a: Number(response1.a),
b: Number(response1.b)
} as Response;
console.log(typeof response2.a); // number
console.log(typeof response2.b); // number
console.log(response2.a + response2.b); // 3
最后我找到了导致错误的原因,我从html模板中获取了页面变量(它是一个输入值),它在函数参数中定义为数字类型,但实际上是一个字符串打字稿无法检查 html 模板中的变量类型,因此当尝试 parseInt(page) 静态打字时会突出显示错误!我通过给页面变量一个“”类型,然后将 parseInt 应用于页面变量来解决这个问题。
我运行遇到类似问题,解决如下:
C:number =0;
A:number=12;
B:number=0.4;
C= Number.parseInt(A.toString()) + Number.parseFloat(B.toString());
console.log("C=" + C );
似乎很愚蠢,将数字转换为字符串并再次解析为数字,但这就是我解决问题的方法。
在数字前加上 +:
let a = +b + +c;
常量值 = Number(stringOrNum)+1;