javascript - Angular2错误: Type 'number' is not assignable to type 'NumberConstructor'

标签 javascript angular typescript

在下面的 Angular2 程序中,我希望系统从 0 或 1 中随机选择四个选项。我得到了所需的输出,并且控制台不报告任何错误。但是,我的 IDE 显示以下错误:

Angular2 Error

我尝试将 Number 更改为 number 但它产生了另一个错误:

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

最佳答案

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/

相关文章:

javascript - 使用 ||运营商通知

php - 仅适用于 iPhone 和常规浏览器的 CSS?

javascript - ReactJS 中的非阻塞渲染

javascript - 选择使用 Angular 2 显示所选值的对象

AngularFireDatabase、Jest 和单元测试,如何创建可重用的类 stub ?

javascript - 将选择选项绑定(bind)到全局变量/数组?

javascript - 使用 styled-components : How to properly use class selectors? 的下拉菜单

html - 用什么代替::ng-deep

javascript - 无法在 Ionic 3 中链式 promise

typescript - TypeScript 中的类型推断丢失了内部级别类型