2016-12-03 315 views
1

在以下Angular2程序中,我希望系統從0或1中隨機選擇四個選項。我得到所需的輸出並且控制檯不會不會報告任何錯誤。然而,我的IDE顯示以下錯誤:Angular2錯誤:類型'數字'不可分配鍵入'NumberConstructor'

Angular2 Error

我試圖改變Numbernumber但它產生另一個錯誤:

Another Error

組件代碼

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); 
    } 
} 

回答

1

替換Numbernumber

switch1Number = Number 

應該

switch1Number: number; 

Number不是一個類型的變量,它是一個數字constructor就像Stringstringconstructor

+0

我監督了另一個錯誤,對不起。類型用':'聲明,'='用於賦值。所以你應該在這裏使用':' –

+0

正確!非常感謝。 – anonym

+0

不客氣:) –

相關問題