最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 Error: Type 'number' is not assignable to type 'NumberConstructor' - Stack

programmeradmin0浏览0评论

In the following Angular2 program where I want the system to randomly choose four choices from either 0 or 1. I'm getting the desired output and the console doesn't report any error. However, my IDE displays the following error:

I tried changing Number to number but it yields another error:

Component Code

import {Component} from 'angular2/core';
import {OnInit} from 'angular2/core';

@Component(
{
    selector: 'puzzle',
    template: `
        <section class="bination">
            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);
    }
}

In the following Angular2 program where I want the system to randomly choose four choices from either 0 or 1. I'm getting the desired output and the console doesn't report any error. However, my IDE displays the following error:

I tried changing Number to number but it yields another error:

Component Code

import {Component} from 'angular2/core';
import {OnInit} from 'angular2/core';

@Component(
{
    selector: 'puzzle',
    template: `
        <section class="bination">
            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);
    }
}
Share Improve this question edited Dec 3, 2016 at 4:22 anonym asked Dec 3, 2016 at 4:12 anonymanonym 4,85012 gold badges51 silver badges76 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Replace Number with number in

switch1Number = Number

it should be

switch1Number: number;

Number is not a type of variable, it's a number constructor just likeString is a string type constructor.

发布评论

评论列表(0)

  1. 暂无评论