2016-12-28 132 views
3

我写了一篇关于问题的新文章以提供更多详细信息。Ionic 2:“属性号”在类型'typeof'上不存在

我尝试在我的离子2项目,创建一个类,我用这个代码:

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

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

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

我想在我的其他组件来使用它。它在我的终端中用“离子服务”进行编译时起作用。但是,当我尝试在我的android手机上编译我有这个错误:

[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! [email protected] build: `ionic-app-scripts build` 
npm ERR! Exit status 1 
npm ERR! 
npm ERR! Failed at the [email protected] 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 

我不明白为什么。首先我在提供者文件上尝试相同的代码,但同样的问题。

有人有什么想法吗?

+0

而不是这样做,只需使用'constructor(private x:number){// ...}'。通过这样做,您不需要在组件中声明'private'属性,因为typescript正在为您声明它。 – sebaferreras

+1

好的,谢谢我改变了这一点。但这不是解决方案。 :) –

+1

当然,这就是为什么它只是一个评论,而不是答案:) – sebaferreras

回答

0

我已经找到了解决方案,但我认为这不是我真正要做的:

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

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

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

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

我的构造留下空的,我写的参数上的另一种方法。这是工作,但如果有人真正纠正我的问题,我会说是! :D

2

当一个类用@component修饰时,这些类是由angular的Dependency Injection框架创建的,它试图从构造函数参数的类型中找到提供者作为找到提供者的关键。

原始类型如string,number,boolean,Object不能用作关键字。我不知道为什么它没有与ionic serve失败。您可以了解更多有关DIhere.

+0

它与离子服务器的工作,因为它不与NGC编译。 –

+0

可能与运行时编译有关。 – raj

相关问题