简体   繁体   中英

Ionic 2 : “Property 'number' does not exist on type 'typeof”

I write a new post about a problem to give more details.

I try to create a class in my ionic 2 project, I use this code :

import { Component } from '@angular/core';

@Component({
    templateUrl: 'field.html'
})

export class test {
    x : number;
    constructor(x : number){
        this.x = x;
    }
}

I want to use it in my other component. It's work when in compile it with "ionic serve" in my terminal. But when I try to compile on my android phone I have this error :

[09:54:06]  Error at C:/xampp/htdocs/AppFineMobile/.tmp/classes/fieldSpecs/test.ngfactory.ts:42:71
[09:54:06]  Property 'number' does not exist on type 'typeof
            "C:/xampp/htdocs/AppFineMobile/.tmp/classes/fieldSpecs/test"'.
[09:54:06]  ngc failed
[09:54:06]  ionic-app-script task: "build"
[09:54:06]  Error: Error

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
npm ERR! node v7.0.0
npm ERR! npm  v3.10.8
npm ERR! code ELIFECYCLE
npm ERR! ionic-hello-world@ build: `ionic-app-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ionic-hello-world@ build script 'ionic-app-scripts build'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the ionic-hello-world package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     ionic-app-scripts build
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs ionic-hello-world
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls ionic-hello-world
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\xampp\htdocs\AppFineMobile\npm-debug.log

I don't understand why. First I try the same code on a provider file, but same problem.

Someone have any idea please ?

When a class is decorated with @component , the classes are created by angular's Dependency Injection framework where it tries to find providers from the type of the constructor parameters as key to find one.

Primitive types like string , number , boolean , Object don't work as keys. I am not sure why it didnt fail with ionic serve . You can learn more about DI here.

I have find a solution, but I think it's not what I really have to do :

import { Component } from '@angular/core';

@Component({
    templateUrl: 'field.html'
})

export class test {
    x : number;
    constructor(){}

    initClass(x : number) {
        this.x = x;
    }
}

My constructor stay empty and I write parameter on another method. It's work, but if someone have the true correction of my problem I say yes ! :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM