0

我对这个问题做了一个更简单的版本。

服务代码:

import { Injectable, ElementRef } from '@angular/core';
import { Subject, Observable, BehaviorSubject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class NotificationService {
private messageSource = new BehaviorSubject<string>('service');
  currentMessage = this.messageSource.asObservable();

  constructor() { 
  }

  changeMessage(message: string) {
    this.messageSource.next(message)
  }
}

订阅服务的组件:

this.notificationService.currentMessage.subscribe(message=> console.log(message));

从库项目中的任何组件触发 changeMes​​sage 时

this.notSrv.changeMessage("TEST");

它工作正常

当尝试从应用程序内部的组件中执行此操作时,它会导入库

imports: [
    ...
    NineGoldLibModule,
    ...
  ],

接下来不触发。

编辑:

九金库.module.ts

import { NgModule, ErrorHandler } 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';
import { ToastrModule } from 'ngx-toastr';
import { BookTrackerErrorHandlerService } from './services/book-tracker-error-handler.service';

@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, ToastrModule.forRoot()
  ],
  exports: [NineGoldLibComponent, NgHeaderComponent, NgStylesComponent, NgFooterComponent, NgNavigationComponent, NgLoginComponent, FormFieldComponent, NgRegisterComponent, NotificationComponent],
  providers: [{ provide: ErrorHandler, useClass: BookTrackerErrorHandlerService }]
})
export class NineGoldLibModule { }

app.module.ts

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
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
4

0 回答 0