programing

Angular 2 모듈에서 인터페이스를 내보낼 수 없습니까?

stoneblock 2023. 7. 8. 10:33

Angular 2 모듈에서 인터페이스를 내보낼 수 없습니까?

인터페이스를 NgModule-declaration으로 내보내고 내보내기를 시도했는데 편집기(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'에서 가져온 인터페이스만 사용할 수 있습니까?

인터페이스를 내보낼 수 없습니다.내보낼 수 있는 항목:

  1. 기타 모듈
  2. 구성 요소들
  3. 지시문
  4. 파이프

NgModule는 각진 개념이므로 형식 스크립트 모듈과 혼동해서는 안 됩니다.당신의 모듈을 사용하는 제3자가 당신의 인터페이스를 사용할 수 있도록 하려면, 당신은 다음을 만들어야 합니다..d.ts모듈과 함께 정의 파일.

다른 NgModule 내의 인터페이스를 사용하려면 다음을 사용해야 합니다.

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

또한 선언 배열에 인터페이스를 넣지 마십시오. 이는 파이프/구성요소/지시에만 사용됩니다.

라이브러리 외부에서 인터페이스를 사용할 수 있도록 하려면 인터페이스를 내보내기에 추가해야 합니다.index.ts:

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

실제로 인터페이스를 내보낼 수는 있지만, 원하는 방식으로는 내보낼 수 없습니다.

먼저 interfaces.ts 파일을 입력하여 생성합니다.

ng g interface <interface_name>

인터페이스 파일의 예:

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

또는

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

만약 당신이 인터페이스를 글로벌하게 공유하는 방법을 알고 있다면 부탁드립니다.여기서 알려주세요: 앱 간에 인터페이스를 공유하는 각도 조절 방법

언급URL : https://stackoverflow.com/questions/42332456/an-interface-cannot-be-exported-in-an-angular-2-module