typescript 如何在打字稿中对2个数字求和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39421955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to sum 2 numbers in typescript
提问by Luis Valencia
I have this properties
我有这个属性
export interface IOurFirstSpfWebPartWebPartProps {
description: string;
n1: number;
n2: number;
}
Then I have this method
然后我有这个方法
public render(): void {
this.domElement.innerHTML = `
<div class="${styles.ourFirstSpfWebPart}">
<div class="${styles.container}">
<div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
<div class="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1">
<span class="ms-font-xl ms-fontColor-white">Welcome to SharePoint!</span>
<p class="ms-font-l ms-fontColor-white">Customize SharePoint experiences using Web Parts.</p>
<p class="ms-font-l ms-fontColor-white">${this.properties.n1} + ${this.properties.n2}</p>
<a href="https://github.com/SharePoint/sp-dev-docs/wiki" class="ms-Button ${styles.button}">
<span class="ms-Button-label">Learn more</span>
</a>
</div>
</div>
</div>
</div>`;
}
However this is printing 1+2 instead of 3.
然而,这是打印 1+2 而不是 3。
回答by Vivek Shukla
Add an extra +
, for every argument which you want to add. You need to do it like: this.sum = +this.num1 + +this.num2;
.
+
为您要添加的每个参数添加一个额外的, 。你需要做的像:this.sum = +this.num1 + +this.num2;
。
回答by the_5imian
You answer is related to implicit coercion in javascript. Simply put, you're concatinating two strings in a template and not adding two numbers.
您的回答与 javascript 中的隐式强制有关。简而言之,您在模板中连接两个字符串,而不是将两个数字相加。
When you hit the {}
s in your templating syntax, your numbers will be stringified. In essence you are then adding
当您{}
在模板语法中点击s 时,您的数字将被字符串化。本质上,您然后添加
"1" + "2" = "12"
“1”+“2”=“12”
instead of
代替
1 + 2 = 3
1 + 2 = 3
You can try two things:
您可以尝试两件事:
- put both expressions inside of the {}
- 将两个表达式放在 {} 中
${this.properties.n1 + this.properties.n2}
${this.properties.n1 + this.properties.n2}
If that still makes "12" then
如果那仍然是“12”,那么
- Try
${parseInt(this.properties.n1) + parseint(this.properties.n2)}
. That will coerce both values to a number first, if it can be done.
- 试试
${parseInt(this.properties.n1) + parseint(this.properties.n2)}
。如果可以的话,这将首先将两个值强制为一个数字。
You can learn more about type coercion in the article You Don't Know JS: Types & Grammar, where it will really explain all the 'gotchas' out there about the + operator and implicit types.
您可以在您不知道的 JS:类型和语法一文中了解有关类型强制的更多信息,其中将真正解释有关 + 运算符和隐式类型的所有“陷阱”。
回答by tygor
The tick mark creates a template literal. I think you're looking for:
${this.properties.n1 + this.properties.n2}
—all inside the same curly braces.
刻度线创建模板文字。我认为您正在寻找:
${this.properties.n1 + this.properties.n2}
- 都在同一个花括号内。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
回答by Marvin Zumbado
prepend the numbers with a + sign
在数字前面加上 + 号