【发布时间】:2017-02-19 20:24:44
【问题描述】:
我尝试在 NgModule 声明中导出接口并导出并在编辑器(Visual Studio 代码)中出现此错误:[ts] 'MyInterface' only refers to a type, but is being used as a value here.
这里是示例代码Edit-1:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MaterialModule } from '@angular/material';
import { MyInterface } from './my.interface';
import { MyService } from './my.service';
@NgModule({
imports: [ CommonModule, FormsModule, MaterialModule.forRoot() ],
declarations: [ MyInterface],//<- this is causing the message
exports: [ MyInterface],
providers: [ MyService ]
})
export class MyModule { }
我找到了in the answer to this post 的部分解释:“因为 TypeScript 中的接口在运行时被擦除”。 我目前正在将我的应用程序重构为功能模块,所以我现在无法对其进行测试:我只能通过从 './mypathto/my.interface' 导入来使用接口吗?
【问题讨论】:
-
发布您的代码。导出和导入接口在这里工作得很好。
-
你想在哪里使用它?作为提供者?
I tried to export an interface in a NgModule-declaration什么意思? -
我已将示例代码添加到问题中。谢谢@yurzui,您的链接正在回答问题:“TypeScript 接口不是有效的令牌”。
标签: angular typescript