Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2错误:类型';编号';不可分配给类型';数字构造器';_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 角度2错误:类型';编号';不可分配给类型';数字构造器';

Javascript 角度2错误:类型';编号';不可分配给类型';数字构造器';,javascript,angular,typescript,Javascript,Angular,Typescript,在下面的Angular2程序中,我希望系统从0或1中随机选择四个选项。我得到了所需的输出,控制台没有报告任何错误。但是,“我的IDE”显示以下错误: 我尝试将Number更改为Number,但它产生了另一个错误: 部件代码 import {Component} from 'angular2/core'; import {OnInit} from 'angular2/core'; @Component( { selector: 'puzzle', template: `

在下面的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);
    }
}
从'angular2/core'导入{Component};
从'angular2/core'导入{OnInit};
@组成部分(
{
选择器:“拼图”,
模板:`
I:{{switch1Number}}
II:{{switch2Number}}
III:{{switch3Number}}
IV:{{switch4Number}} ` }) 导出类组件实现OnInit{ 开关1编号=编号; 开关2编号=编号; 开关3编号=编号; 开关4NUMBER=数字; 恩戈尼尼特(){ //randon给出了一个介于0和1之间的随机十进制值。 //数学..四舍五入到0或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); } }
将中的
编号
替换为
编号

switch1Number = Number
应该是

switch1Number: number;

Number
不是一种变量类型,它是一个数字
constructor
就像
String
是一个
String
type
constructor

对不起,我发现了另一个错误。类型用
声明,而
=
用于赋值。所以你应该在这里使用
正确!谢谢。