1
在以下Angular2程序中,我希望系統從0或1中隨機選擇四個選項。我得到所需的輸出並且控制檯不會不會報告任何錯誤。然而,我的IDE顯示以下錯誤:Angular2錯誤:類型'數字'不可分配鍵入'NumberConstructor'
我試圖改變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);
}
}
我監督了另一個錯誤,對不起。類型用':'聲明,'='用於賦值。所以你應該在這裏使用':' –
正確!非常感謝。 – anonym
不客氣:) –