[英]TypeScript compiles, but gives a runtime error
I am very new to TypeScript and I've run into a problem that I can't quite figure out...我是 TypeScript 的新手,遇到了一个我不太明白的问题......
I am using a library called TypeLite that will take my C# POCO's and convert them into TypeScript classes.我正在使用一个名为 TypeLite 的库,它将采用我的 C# POCO 并将它们转换为 TypeScript 类。
It is a T4 template that generates a file called TypeLite.Net4.d.ts, which as I understand, .d files are definition files that are loaded automatically.它是一个 T4 模板,生成一个名为 TypeLite.Net4.d.ts 的文件,据我了解,.d 文件是自动加载的定义文件。
The generated code looks like this:生成的代码如下所示:
declare module Models {
export class LoginModel {
password: string;
rememberMe: boolean;
userName: string;
}
}
In my component, I can access Models.LoginModel just fine and it doesn't give me any compiler errors (using Visual Studio).在我的组件中,我可以很好地访问 Models.LoginModel 并且它不会给我任何编译器错误(使用 Visual Studio)。
However, when I try and run it, I get:但是,当我尝试运行它时,我得到:
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
Here is my LoginComponent:这是我的登录组件:
import { Component } from 'angular2/core';
@Component({
selector: 'login',
templateUrl: './app/login/login.html'
})
export class LoginComponent {
model: Models.LoginModel = new Models.LoginModel();
}
What am I doing wrong here?我在这里做错了什么?
you have not imported Model into your LoginComponent您还没有将模型导入您的 LoginComponent
import { Model } from 'path to Model module';
The intellisense you might be getting because of the definition file, but to use it at run time you have to import the class.由于定义文件,您可能会获得智能感知,但要在运行时使用它,您必须导入该类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.