带有普通 TypeScript 的 Angular 6 - 错误:模块没有导出成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50647724/
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 05:27:50  来源:igfitidea点击:

Angular 6 with plain TypeScript - ERROR: module has no exported member

angulartypescriptenums

提问by Isabelle Plante

I have a small Angular 6 library in which I am building a reusable component to display page-level messages, such as error, info, warning, and success.

我有一个小的 Angular 6 库,我正在其中构建一个可重用的组件来显示页面级消息,例如错误、信息、警告和成功。

For this I have an enum ServerMessageType.ts, which is simple enough:

为此,我有一个 enum ServerMessageType.ts,它很简单:

export enum ServerMessageType {
    Error = 'error',
    Info = 'info',
    Success = 'success',
    Warning = 'warning'
}

ServerMessage.tsshould use ServerMessageType:

ServerMessage.ts应该使用ServerMessageType

import { ServerMessageType } from './ServerMessageType';

export class ServerMessage {
    constructor(public type: ServerMessageType, 
                public messageText: string, 
                public dismissible?: boolean, 
                public id?: string) {
    }
}

server-messages.component.ts

server-messages.component.ts

import { Component, EventEmitter, Input, Output } from '@angular/core';

import { ServerMessage } from './ServerMessage';
import { ServerMessageType } from './ServerMessageType';

    @Component({
  selector: 'app-server-messages',
  templateUrl: './server-messages.component.html',
  styleUrls: []
})
export class ServerMessagesComponent {

  @Input() messages: ServerMessage[];
  @Output() messageRemoved = new EventEmitter<ServerMessage>();

  constructor() {
  }

  clearMessage = function (index: number) {
    const message = this.messages[ index ];

    this.messageRemoved.emit(message);
    this.messages.splice(index, 1);
  };

  getMessageClass(message: ServerMessage): string {
    switch (message.type) {
      case ServerMessageType.Error:
        return 'alert-danger';
      case ServerMessageType.Info:
        return 'alert-info';
      case ServerMessageType.Success:
        return 'alert-success';
      case ServerMessageType.Warning:
        return 'alert-warning';
    }
  }

  isDismissible(message: ServerMessage): boolean {
    return message.dismissible === true;
  }
}

However, when I run ng build, I get an error:

但是,当我运行时ng build,出现错误:

BUILD ERROR  
> projects/framework/src/lib/server-messages/server-messages.component.ts(3,28):
> error TS2305: Module
> '"/Users/xxx/dev/apps/framework/projects/framework/src/lib/server-messages/ServerMessage"'
> has no exported member 'ServerMessageType'

What am I doing wrong? If I change typein ServerMessageto be a string and remove the ServerMessageTypeenum altogether, things build and deploy just fine.

我究竟做错了什么?如果我改变typeServerMessage是一个字符串,删除ServerMessageType完全枚举,事情构建和部署就好了。

Thanks!

谢谢!

采纳答案by Isabelle Plante

Stupid mistake!

愚蠢的错误!

In server-messages.component.ts, these two lines:

server-messages.component.ts 中,这两行:

import { ServerMessage } from './ServerMessage';
import { ServerMessageType } from './ServerMessageType';

Had been reverted to

已恢复为

import { ServerMessage, ServerMessageType } from './ServerMessage';

Thanks for your help! It couldn't find ServerMessageTypein ServerMessage.ts, since it's exported in ServerMessageType.ts...

谢谢你的帮助!它ServerMessageTypeServerMessage.ts找不到,因为它是在ServerMessageType.ts 中导出的...

Thanks all for your help!

感谢你的帮助!

回答by mittal bhatt

You need to import your component in your root module and when you make service than make it @Injectable than use.

您需要在您的根模块中导入您的组件,并且当您提供服务时,将其设置为 @Injectable 而不是使用。

回答by Bert Verhees

You need to explicitly export a component from a module if you want to import that component by importing that module.

如果您想通过导入该模块来导入该组件,则需要从模块中显式导出组件。

The code in which you must solve this problem is the module-code from which you want to export that component.

您必须解决此问题的代码是您要从中导出该组件的模块代码。