接口不能在角度2模块中导出? [英] An interface cannot be exported in an angular 2 module?

查看:55
本文介绍了接口不能在角度2模块中导出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在NgModule声明中导出接口并导出,并且在编辑器中已经出现此错误(Visual Studio代码):[ts] 'MyInterface' only refers to a type, but is being used as a value here.

I tried to export an interface in a NgModule-declaration and export and getting this error already in the editor (Visual Studio Code): [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 { }

在这篇文章的答案中,我找到了的解释的一部分:由于在TypeScript中在运行时擦除了接口, ". 我目前正在将我的应用重构为功能模块,所以我现在无法对其进行测试:我可以仅通过从"./mypathto/my.interface"导入来使用这些接口吗?

One part of an explanation I found in the answer to this post: "since interfaces are erased at runtime in TypeScript". I'm currently refactoring my app to feature modules, so I cannot test it right now: Can I use the interfaces only by import from './mypathto/my.interface'?

推荐答案

您不能导出接口.您只能导出:

You cannot export an interface. You can only export:

  1. 其他模块
  2. 组件
  3. 指令
  4. 管道

NgModule是一个角度概念,不应与打字稿模块混淆.要使使用您的模块的第三方能够使用您的界面,您应该在模块中创建一个.d.ts定义文件.

NgModule is an angular concept and should not be confused with a typescript module. To make a third party, who uses your module, able to use your interface, you should create a .d.ts definition file with your module.

如果要在另一个NgModule中使用接口,则应使用:

If you want to use a interface inside another NgModule of yours, you should just use:

import {InterfaceName} from 'path/to/ngmodule/to/interface';

也不要在声明数组中放置接口,这仅用于管道/组件/指令.

Also, do not put an interface in the declarations array, this is only used for pipes/components/directives.

如果希望接口在库外部可用,则应将其添加到index.ts的导出中:

If you want your interface to be usable outside of an library, you should add it to the export of your index.ts:

export {InterfaceName} from 'path/to/ngmodule/to/interface';

这篇关于接口不能在角度2模块中导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆