typescript 无法在 angular 2 模块中导出接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42332456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:16:27  来源:igfitidea点击:

An interface cannot be exported in an angular 2 module?

angulartypescript

提问by Myonara

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.

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

Here is the example code Edit-1:

这是示例代码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 { }

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'?

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

回答by Poul Kruijt

You cannot export an interface. You can only export:

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

  1. Other modules
  2. Components
  3. Directives
  4. Pipes
  1. 其他模块
  2. 组件
  3. 指令
  4. 管道

NgModuleis 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.tsdefinition file with your module.

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

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

如果你想在你的另一个 NgModule 中使用一个接口,你应该使用:

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.

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

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

如果您希望您的界面在库之外可用,您应该将其添加到您的导出中index.ts

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

回答by trojan

Well actually you can export an interface, but not the way you are trying to do.

好吧,实际上您可以导出接口,但不能以您尝试的方式导出。

First generate interfaces.ts file by typing

首先通过键入生成 interfaces.ts 文件

ng g interface <interface_name>

ng g interface <interface_name>

Example of interface file:

接口文件示例:

export interface Auction{ $key?:string; $auctioneer:string;  ... }
export interface Item{ $key?:string; condition?:string; description?:string; ...}
export interface Bid{  $key?:string;  amount:number;  auction:string; ...}

After modifying the file to your needs you can import just one, all or chosen interfaces like this:

根据您的需要修改文件后,您可以只导入一个、所有或选定的接口,如下所示:

import { Auction } from './interfaces';

or

或者

import * as interfaces from './interfaces'; 
// then you have to call interfaces.Auction

If per chance you know how to share interfaces globally pls. let me know here: Angular how to share interfaces across the app

如果有机会您知道如何全局共享接口。让我知道:Angular 如何在应用程序中共享界面