angular:如何从npm包导出服务

时间:2017-07-04 07:22:42

标签: angular npm

我的Angular 2软件包中有一项服务,我希望将这些服务放在NPM上:

public showModal: Subject<any> = new Subject<any>();
public $showModal = this.showModal.asObservable();

public closeModal: Subject<any> = new Subject<any>();
public $closeModal = this.closeModal.asObservable();

public configModal: Subject<any> = new Subject<any>();
public $configModal = this.configModal.asObservable();

主题变量的使用方式如下:myService.configModal(new ModalConfig()); Observable与订阅一起使用来更新我的组件数据,显示或隐藏它。

这是我的index.ts导出我的班级:

import {NgModule} from "@angular/core";
import {CommonModule} from "@angular/common";
import {ModalComponent} from "./lib/modal.component";
import {ModalService} from "./lib/modal.service";
@NgModule({
    imports: [CommonModule],
    declarations: [ModalComponent],
    providers: [ModalService],
    exports: [ModalComponent],
//  bootstrap: [ModalComponent]
})
export class ModalModule {}

如何在其他应用中访问ModalService?我希望我的服务也是一个单身人士,所以我无法导出它并在我的组件中提供服务

1 个答案:

答案 0 :(得分:3)

几天后,我找到了解决问题的方法。我只需要添加一个导出语句:

import {NgModule} from "@angular/core";
import {CommonModule} from "@angular/common";
import {ModalComponent} from "./lib/modal.component";
import {ModalService} from "./lib/modal.service";

export {ModalService} from "./lib/modal.service";

@NgModule({
    imports: [CommonModule],
    declarations: [ModalComponent],
    providers: [ModalService],
    exports: [ModalComponent],
//  bootstrap: [ModalComponent]
})
export class ModalModule {}