Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在angular 2模块中导出接口?_Angular_Typescript - Fatal编程技术网

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

无法在angular 2模块中导出接口?,angular,typescript,Angular,Typescript,我尝试在NgModule声明中导出接口并导出,但编辑器中已出现此错误(Visual Studio代码):[ts]“MyInterface”仅引用类型,但在此处用作值。 下面是示例代码Edit-1: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/

我尝试在NgModule声明中导出接口并导出,但编辑器中已出现此错误(Visual Studio代码):[ts]“MyInterface”仅引用类型,但在此处用作值。

下面是示例代码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 { }
从'@angular/core'导入{NgModule};
从“@angular/common”导入{CommonModule};
从'@angular/forms'导入{FormsModule};
从“@angular/material”导入{MaterialModule};
从“/my.interface”导入{MyInterface};
从“/my.service”导入{MyService};
@NGD模块({
导入:[CommonModule、FormsModule、MaterialModule.forRoot()],

声明:[MyInterface],//不能导出接口。只能导出:

  • 其他模块
  • 组成部分
  • 指令
  • 管道
  • NgModule
    是一个角度概念,不应与typescript模块混淆。要使使用您的模块的第三方能够使用您的界面,您应该为您的模块创建一个
    .d.ts
    定义文件

    如果您想在您的另一个NgModule内使用接口,您只需使用:

    import {InterfaceName} from 'path/to/ngmodule/to/interface';
    
    另外,不要在声明数组中放置接口,这仅用于管道/组件/指令

    如果希望界面在库之外可用,则应将其添加到
    索引.ts的导出中:

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

    实际上,您可以导出一个接口,但不是您尝试的方式

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

    ng g接口

    接口文件示例:

    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; ...}
    
    根据需要修改文件后,您可以只导入一个、所有或选定的界面,如下所示:

    import { Auction } from './interfaces';
    


    如果你有机会知道如何在全球范围内共享接口,请在这里告诉我:

    发布你的代码。导出和导入接口在这里工作正常。你想在哪里使用它?作为提供者?
    我尝试在NgModule声明中导出接口
    你的意思是什么?看看我添加了示例代码的文档问题。谢谢@yurzui,你的链接回答了这个问题:“类型脚本接口不是有效的令牌”。谢谢PierreDuc的“完整答案”,这澄清了很多。我发现。但是,我如何与第三方沟通,传入库的数据应该遵循某种模式,我希望在接口中指定这种模式?这将使它更容易,例如在npm上发布节点包时,不是吗?
    import * as interfaces from './interfaces'; 
    // then you have to call interfaces.Auction