admin管理员组文章数量:1202391
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="" 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.
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.
Share Improve this question asked Sep 10, 2016 at 2:31 Luis ValenciaLuis Valencia 34k99 gold badges308 silver badges531 bronze badges 1- Because + also works as a concatenation operator and that's what seems to be done in your code.So its printing 1+2 and not 3. instead try adding your expression in curly braces it will work. – Manish Commented Sep 10, 2016 at 2:34
4 Answers
Reset to default 15Add an extra +
, for every argument which you want to add. You need to do it like: this.sum = +this.num1 + +this.num2;
.
You answer is related to implicit coercion in javascript. Simply put, you're concatinating two strings in a template and not adding two numbers.
When you hit the {}
s in your templating syntax, your numbers will be stringified. In essence you are then adding
"1" + "2" = "12"
instead of
1 + 2 = 3
You can try two things:
- put both expressions inside of the {}
${this.properties.n1 + this.properties.n2}
If that still makes "12" then
- Try
${parseInt(this.properties.n1) + parseint(this.properties.n2)}
. That will coerce both values to a number first, if it can be done.
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.
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.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
prepend the numbers with a + sign
本文标签: javascripthow to sum 2 numbers in typescriptStack Overflow
版权声明:本文标题:javascript - how to sum 2 numbers in typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738616971a2102965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论