2016-05-13 162 views
1

我很新的打字稿和我碰到,我不能完全弄清楚的一個問題...打字稿編譯,但給出了一個運行時錯誤

我使用了一個名爲TypeLite庫,將採取我的C#POCO's並將它們轉換爲TypeScript類。

這是一個T4模板,它生成一個名爲TypeLite.Net4.d.ts的文件,據我瞭解,.d文件是自動加載的定義文件。

生成的代碼看起來是這樣的:

declare module Models { 
    export class LoginModel { 
     password: string; 
     rememberMe: boolean; 
     userName: string; 
    } 
} 

在我的部分,我可以訪問Models.LoginModel就好了,它並沒有給我任何編譯器錯誤(使用Visual Studio)。

然而,當我嘗試運行它,我得到:

EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in :0:0 
ORIGINAL EXCEPTION: ReferenceError: Models is not defined 
ORIGINAL STACKTRACE: 
ReferenceError: Models is not defined 
    at new LoginComponent 

這裏是我的LoginComponent:

import { Component } from 'angular2/core'; 

@Component({ 
    selector: 'login', 
    templateUrl: './app/login/login.html' 
}) 

export class LoginComponent { 
    model: Models.LoginModel = new Models.LoginModel(); 
} 

我在做什麼錯在這裏?您還沒有導入模型到您的LoginComponent

import { Model } from 'path to Model module'; 

你可能會因爲定義文件的越來越智能感知,但在運行時使用它

回答

2

你必須導入類。

希望這有助於!