简体   繁体   English

TypeScript 可以编译,但会出现运行时错误

[英]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.

相关问题 Typescript在运行时给出错误,但在编译时不给出 - Typescript gives error during runtime but not on compile time 为什么在Typescript类中的这种结构在编译时会很好地导致运行时错误? - Why is this structure in typescript class resulting in a runtime error when it compiles just fine? TypeScript:从等待的Promise进行数组解构编译并在运行时崩溃 - TypeScript: array destructuring from awaited Promise compiles and crash at runtime Typescript 显示错误,即使它在 VSCode 上编译也是如此 - Typescript shows an error even though it compiles on VSCode TypeScript 编译成功但 output JS 显示错误 - TypeScript compiles successfully but output JS shows error Angular ngFor 在 svg 中给出错误,即使它可以编译 - Angular ngFor gives an error in svg, even though it compiles 跨文件跨越一个Typescript模块会产生0x800a01bd - JavaScript运行时错误:对象不支持此操作 - Spanning one Typescript module across files gives 0x800a01bd - JavaScript runtime error: Object doesn't support this action 在打字稿组件中使用“ import * as”会产生错误- - Using 'import * as' in a typescript component gives an error - 使用TypeScript的combineEpics会出现类型错误 - combineEpics using TypeScript gives type error 任何类型的打字稿类型都会在Ionic 2产生错误 - Typescript type of any gives error at Ionic 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM