2016-12-28 85 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

相關問題