在Angular 2中导入界面

Jam*_*ton 11 interface typescript angular

在使用Angular 2的Meteor应用程序中,我想创建一个自定义数据类型,如下所示:

interface MyCustomType {
  index: number;
  value: string;
}
Run Code Online (Sandbox Code Playgroud)

然后我想在多个文件中使用此自定义类型.我尝试创建一个名为"mycustom.type.ts"的单独文件,其中包含以下内容:

export interface MyCustomType {
  index: number;
  value: string;
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试导入此类型,以便它可以在另一个文件中使用:

import MyCustomType from "./mycustom.type"
Run Code Online (Sandbox Code Playgroud)

但是,Atom报告以下错误:

TS Error File '/.../mycustom.type.ts' is not a module ...
Run Code Online (Sandbox Code Playgroud)

我应该如何声明和导入类型,以便它们可以在多个地方使用?

max*_*992 20

您应该像这样导入它:

import { MyCustomType } from './mycustom.type';
Run Code Online (Sandbox Code Playgroud)

别忘了{}.


edz*_*ion 12

我正在添加这个答案,因为接受的答案是不完整的.你有两个问题:

一,您需要添加export到您的界面,以便您可以将import其作为模块:

export interface MyCustomType { index: number; value: string; }

其次,您需要将大括号添加{ }到import语句中:

import { MyCustomType } from './mycustom.type';

  • @alibenmessaoud用于`import`它描述了从文件导入哪些模块.例如,来自'./ modulefile'的`import {ModuleA,ModuleB,ModuleC} (2认同)