Angular 9 Service as Obesrvable in library

不想你离开。 提交于 2021-01-29 19:56:24

问题


I am working on multiapplication setup, where inside workspace 2 or 3 applications will share library with common and shared components.

Currently, I am adding notifications service. Added (at least for now) as part of library main module. As base I am using this example from stackblitz or this one

When calling notification service from inside library (which is inside the same module as notification.component:

this._notificationSvc.success('Hello World','This is a success !');

the service fire next event and everything works fine.

Now, when I try the same from application code, the service function works (the below code executes):

success(title: string, message: string, timeout = 3000) {
    this.subject.next(new Notification(this.id++, NotificationType.success, title, message, timeout));
  }

but the next event from service is not fired.

I know that the issue is probably with dependency injection. While I feel quite comfortable with sharing components between modules, I cannot fully get how should this work with services.

Other services that are not Observables work fine so far.

Code: notification.service - I left provided in 'root'

@Injectable({
  providedIn: 'root'
})

but I also tried provided in NineGoldLibModule or AppModule

My library module nine-gold-lib.module.ts looks like this:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NineGoldLibComponent } from './nine-gold-lib.component';
import { NgHeaderComponent } from './common/ng-header/ng-header.component';
import { NgStylesComponent } from './common/ng-styles/ng-styles.component';
import { NgFooterComponent } from './common/ng-footer/ng-footer.component';
import { NgNavigationComponent } from './shared/ng-navigation/ng-navigation.component';
import { RouterModule } from '@angular/router';
import { NgLoginComponent } from './shared/forms/ng-login/ng-login.component';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormFieldComponent } from './directives/form-field/form-field.component';
import { NgSelectModule } from '@ng-select/ng-select';
import { CollapseModule } from 'ngx-bootstrap/collapse';
import { AccordionModule } from 'ngx-bootstrap/accordion';
import { ModalModule } from 'ngx-bootstrap/modal';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { NgRegisterComponent } from './shared/forms/ng-register/ng-register.component';
import { NotificationComponent } from './shared/notification/notification.component';

@NgModule({
  declarations: [NineGoldLibComponent, NgHeaderComponent, NgStylesComponent, NgFooterComponent, NgNavigationComponent, NgLoginComponent, FormFieldComponent, NgRegisterComponent, NgRegisterComponent, NotificationComponent],
  imports: [RouterModule, CommonModule, ReactiveFormsModule, HttpClientModule, BsDropdownModule.forRoot(), BrowserAnimationsModule, NgSelectModule, CollapseModule.forRoot(),
    AccordionModule.forRoot(), ModalModule.forRoot(), FontAwesomeModule
  ],
  exports: [NineGoldLibComponent, NgHeaderComponent, NgStylesComponent, NgFooterComponent, NgNavigationComponent, NgLoginComponent, FormFieldComponent, NgRegisterComponent, NotificationComponent]
})
export class NineGoldLibModule { }

and application module:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NineGoldLibModule } from 'nine-gold-lib';
import { LoginComponent } from './pages/login/login.component';
import { GeneratorComponent } from './pages/generator/generator.component';
import { ToolsComponent } from './pages/tools/tools.component';
import { DownloadsComponent } from './pages/downloads/downloads.component';
import { AdminComponent } from './pages/admin/admin.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ReactiveFormsModule } from '@angular/forms';
import { CollapseModule } from 'ngx-bootstrap/collapse';
import { AccordionModule } from 'ngx-bootstrap/accordion';
import { NgSelectModule } from '@ng-select/ng-select';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    GeneratorComponent,
    ToolsComponent,
    DownloadsComponent,
    AdminComponent

  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NineGoldLibModule,
    BrowserAnimationsModule,
    ReactiveFormsModule,
    CollapseModule.forRoot(),
    AccordionModule.forRoot(),
    NgSelectModule,
    FontAwesomeModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Also there is public-api.ts for library:

export * from './lib/nine-gold-lib.service';
export * from './lib/nine-gold-lib.component';
export * from './lib/nine-gold-lib.module';
export * from './lib/common/ng-header/ng-header.component';
export * from './lib/common/ng-styles/ng-styles.component';
export * from './lib/common/ng-footer/ng-footer.component';
export * from './lib/shared/ng-navigation/ng-navigation.component';
export * from './lib/shared/forms/ng-login/ng-login.component';
export * from './lib/directives/form-field/form-field.component';
export * from './lib/shared/forms/ng-register/ng-register.component';
export * from './lib/shared/notification/notification.component';

Edit:

I found one possible solution for this. Inside each application that uses Notification Service I add service that extends the service from Library:

import { Injectable } from '@angular/core';
import { NotificationService } from 'nine-gold-lib';

@Injectable()
export class GeneratorNotificationService extends NotificationService {

  constructor() {
    super();
  }
}

and provide it in app module:

providers: [{provide: GeneratorNotificationService, useExisting: NotificationService}],

I want to make sure this is correct way to tackle this, so I leave this question open for another few days.

来源:https://stackoverflow.com/questions/62150295/angular-9-service-as-obesrvable-in-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!