微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何从 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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。