如何从 angular 的模块文件中调用服务内部编写的函数?

问题描述

我需要从 app.modulesharedService 调用一个函数。通常我们通过添加依赖注入并在类中调用它来实现。但我这里的情况不同。当 ngxs 商店被添加app.module 中的提供者时,我想从服务文件中设置一个值。除了从 sharedService 调用函数 getAcceptedValue() 以获得我想要的值之外,我没有任何其他选择。 我想在模块的提供者部分调用 performStoreOperations

import {SharedService} from './shared.service';

@NgModule({
  imports: [
    NgxsModule.forFeature([
      AuthState,]),NgxsResetPluginModule.forRoot(),SharedModule,NgxsReduxDevtoolsPluginModule.forRoot(),],declarations: [AppComponent],bootstrap: [AppComponent],providers: [
    {
      provide: NGXS_PLUGINS,useValue: performStoreOperations,// How to call performStoreOperations function here ???????
      multi: true,},})
export class AppModule {
  constructor(private shared:SharedService){}

  performStoreOperations(state,action,next) {
  {
    let acceptedValue = this.shared.getAcceptedValue(); 
    localStorage.setItem('test',acceptedValue);
    return next(state,action);
  }
}

有人可以帮忙吗?

解决方法

你可以这样做。

来自模块 A 的共享服务


    scroll(el: HTMLElement,behaviour: any = "smooth",block: any = "start",inline: any = "nearest") {
        el.scrollIntoView({ behavior: behaviour,block: block,inline: inline })
      }

模块 A

    @NgModule({
      declarations: [],imports: [//some components],exports: [//some components],providers: [
        SharedService,]
    })
    export class SharedModule { }

some.component.ts

    import {sharedService} from 'your/path/t
    constructor(private sharedService: SharedService){}
    //do something with this.sharedService.scroll(param1,...........etc)