2016-04-24 150 views
8

創建一個Angular2應用程序時,在第一個類的構造函數中調用另一個類的構造函數時,我遇到了以下問題。Typescript class.default不是構造函數

頭等艙代碼

import {SecondClass} from './second-class' 

export class FirstClass { 
    someVar:string; 
    secondClass:SecondClass; 

    constructor(firstClass?: FirstClass) { 
     this.someVar='test'; 
     this.secondClass= new SecondClass(); 
    } 
} 

二等代碼:

export class SecondClass { 
    someOtherVar:string; 

    constructor(secondClass?:SecondClass) { 
     this.someOtherVar='test'; 
    } 
} 

能給我的錯誤:原來的異常:類型錯誤:second_class_1.default不是構造函數的

內容。/second-class

System.register([], function(exports_1, context_1) { 
    "use strict"; 
    var __moduleName = context_1 && context_1.id; 
    var SecondClass; 
    return { 
     setters:[], 
     execute: function() { 
      SecondClass = (function() { 
       function SecondClass(secondClass) { 
        this.someOtherVar='test'; 
       } 
       return SecondClass; 
      }()); 
      exports_1("SecondClass", SecondClass); 
     } 
    } 
}); 
//# sourceMappingURL=second-class.js.map 

這是來自Typescript編譯器的編譯輸出

+0

發佈內容。 – dfsq

+0

'default'是一個[javascript保留字](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords)。 – Mottie

+0

SecondClass可能出現循環錯誤 – randominstanceOfLivingThing

回答

12

錯誤消息意味着您在./second-class(非默認值)中使用了命名導出(export class SecondClass {})。因此,這意味着你應該進口看起來像

import {SecondClass} from './second-class' 
+0

謝謝。修改了代碼,但仍然收到錯誤,如ORIGINAL EXCEPTION:TypeError:second_class_1.SecondClass不是構造函數 –

+0

您可以發佈'./second-class'的內容嗎? (TypeScript,沒有轉發)。 – dfsq

+0

我在剛纔FirstClass代碼下面的問題中使用它。 –

4

有在代碼中的一些錯誤:從調用構造函數

  • 進口

  • 失蹤()失蹤{}

  • 缺少this訪問班級成員

頭等艙代碼

import {SecondClass} from './second-class' 

export class FirstClass { 
    someVar:string; 
    secondClass:SecondClass; 

    constructor(firstClass?: FirstClass) { 
     this.someVar='test'; 
     this.secondClass= new SecondClass(); 
    } 
} 

二等代碼:`/第二class`的

export class SecondClass { 
    someOtherVar:string; 

    constructor(secondClass?:SecondClass) { 
     this.someOtherVar='test'; 
    } 
} 
+0

謝謝。這些是輸入問題時的拼寫錯誤。沒有這些錯誤的代碼給出了所述錯誤。 –