typescript 使用 forRoot 传递配置数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43292628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:25:54  来源:igfitidea点击:

Pass config data using forRoot

angulartypescriptangular2-servicesangular2-aot

提问by Michael Doye

I am trying to pass config data into a custom library in Angular.

我正在尝试将配置数据传递到 Angular 中的自定义库中。

In the users application, they will pass some config data to my library using forRoot

在用户应用程序中,他们将使用一些配置数据传递给我的库forRoot

// Import custom library
import { SampleModule, SampleService } from 'custom-library';
...

// User provides their config
const CustomConfig = {
  url: 'some_value',
  key: 'some_value',
  secret: 'some_value',
  API: 'some_value'
  version: 'some_value'
};

@NgModule({
  declarations: [...],
  imports: [
    // User config passed in here
    SampleModule.forRoot(CustomConfig),
    ...
  ],
  providers: [
    SampleService
  ]
})
export class AppModule {}


In my custom library, specifically the index.ts, I can access the config data:

在我的自定义库中,特别是index.ts,我可以访问配置数据:

import { NgModule, ModuleWithProviders } from '@angular/core';
import { SampleService } from './src/sample.service';
...

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [...],
  exports: [...]
})
export class SampleModule {
  static forRoot(config: CustomConfig): ModuleWithProviders {
    // User config get logged here
    console.log(config);
    return {
      ngModule: SampleModule,
      providers: [SampleService]
    };
  }
}

My question is how do I make the config data available in the custom library's SampleService

我的问题是如何使自定义库中的配置数据可用 SampleService

Currently SampleServicecontains the following:

目前SampleService包含以下内容:

@Injectable()
export class SampleService {

  foo: any;

  constructor() {
    this.foo = ThirdParyAPI(/* I need the config object here */);
  }

  Fetch(itemType:string): Promise<any> {
    return this.foo.get(itemType);
  } 
}

I have read through the docs on Providers, however the forRootexample is quite minimal and doesn't seem to cover my use case.

我已经通读了Providers上的文档,但是这个forRoot例子非常小,似乎没有涵盖我的用例。

回答by Günter Z?chbauer

You are almost there, simply provide both SampleServiceand configin your module like below:

您就快到了,只需在您的模块中同时提供SampleServiceconfig,如下所示:

export class SampleModule {
  static forRoot(config: CustomConfig): ModuleWithProviders<SampleModule> {
    // User config get logged here
    console.log(config);
    return {
      ngModule: SampleModule,
      providers: [SampleService, {provide: 'config', useValue: config}]
    };
  }
}
@Injectable()
export class SampleService {

  foo: string;

  constructor(@Inject('config') private config:CustomConfig) {
    this.foo = ThirdParyAPI( config );
  }
}

Update:

更新

Since Angular 7 ModuleWithProvidersis generic, so it needs ModuleWithProviders<SampleService>

由于 Angular 7ModuleWithProviders是通用的,所以它需要ModuleWithProviders<SampleService>