Angular库的发布和订阅模式实现

时间:2019-04-03 08:19:32

标签: angular microservices micro-frontend

场景:我有2个库项目,即lib1和lib2,以及一个应用程序项目,即lib-app,它使用了先前创建的2个库。

在lib-app中,我已经使用服务类实现了发布和订阅模式。

在lib-app notificationService中

export class NotificationService {
    /*Publish method*/

    emitActiveEditor$: Subject<any> = new BehaviorSubject<any>(null);
      emitActiveEditorInfo(any: any) {
        this.emitActiveEditor$.next(any);
      }
    /*subscribe mehthod*/

      get emitActiveEditorChange(): BehaviorSubject<any> {
        return (this.emitActiveEditor$ as BehaviorSubject<any>);
    } 
}

现在在lib1库中,我发布如下消息

import { NotificationService } from 'dist/muvi6-utilities';**//this import show error**
    constructor(private notificationService: NotificationService) { }
       ngOnInit() {
           this.notificationService.emitActiveEditorChange();
          }

在lib2中,我订阅

import { NotificationService } from 'dist/muvi6-utilities';**//this import show error**
    constructor(private notificationService: NotificationService) { }

       ngOnInit() {
    //this.notificationService.emitActiveEditorChange();

    this.notificationService.emitActiveEditorChange.subscribe(result => {
      if (result) {
        alert("Hello Received in Lib2" +result)

      }
    });
  }

在这里,我无法导入NotificationService表单Lib-app

谁能告诉我该怎么做? 基本上,我想从lib1中的组件发布并在lib2中订阅它,反之亦然。

0 个答案:

没有答案