在下面的 Angular2 程序中,我希望系统从 0 或 1 中随机选择四个选项。我得到了所需的输出,并且控制台不报告任何错误。但是,我的 IDE 显示以下错误:
我尝试将 Number
更改为 number
但它产生了另一个错误:
组件代码
import {Component} from 'angular2/core';
import {OnInit} from 'angular2/core';
@Component(
{
selector: 'puzzle',
template: `
<section class="combination">
I: {{ switch1Number }}<br>
II: {{ switch2Number }}<br>
III: {{ switch3Number }}<br>
IV: {{ switch4Number }}
</section>
`
})
export class PuzzleComponent implements OnInit {
switch1Number = Number;
switch2Number = Number;
switch3Number = Number;
switch4Number = Number;
ngOnInit() {
// Math.randon gives a random decimal value between 0 & 1.
// Math..round rounds it to 0 or 1
this.switch1Number = Math.round(Math.random());
this.switch2Number = Math.round(Math.random());
this.switch3Number = Math.round(Math.random());
this.switch4Number = Math.round(Math.random());
console.log(this.switch1Number, this.switch2Number, this.switch3Number, this.switch4Number);
}
}
最佳答案
将 Number
替换为 number
中的
switch1Number = Number
应该是
switch1Number: number;
Number
不是变量类型,它是一个数字构造函数
,就像String
是一个string
类型构造函数
。
关于javascript - Angular2错误: Type 'number' is not assignable to type 'NumberConstructor' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40944150/