Búsqueda de sitios web

javascript - Ionic 2: "La propiedad 'number' no existe en el tipo 'typeof'"


Escribo un nuevo artículo sobre un problema para dar más detalles.

Intento crear una clase en mi proyecto de Ionic 2, uso este código:

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

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

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

Quiero usarlo en mi otro componente. Funciona cuando lo compilo con "ionic serve" en mi terminal. Pero cuando intento compilarlo en mi teléfono Android tengo este 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

No entiendo por qué. Primero intento el mismo código en un archivo de proveedor, pero el mismo problema.

¿Alguien tiene alguna idea, por favor?

Respuesta #1

Cuando una clase está decorada con @component, las clases son creadas por el Dependency Injection de Angular, donde se intenta encontrar proveedores a partir del tipo de los parámetros del constructor como clave para encontrar uno.

Los tipos primitivos como string, number, boolean, Object no funcionan como claves. No estoy seguro de por qué no falló con ionic serve. Puedes aprender más sobre DI aquí.

Respuesta #2

He encontrado una solución, pero creo que no es lo que realmente tengo que hacer :

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

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

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

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

Mi constructor permanece vacío y escribo el parámetro en otro método. Funciona, ¡pero si alguien tiene la verdadera corrección de mi problema, digo que sí! :D