Angular 角度依赖提供程序

Angular 角度依赖提供程序,angular,Angular,我有一个带有参数PATH的服务,我需要在模块的提供者中设置此服务时设置此参数 当我将此服务注入此模块的某个组件并调用该服务的任何方法时,参数PATH将已设置 我跟着这个医生,但对我没有帮助 @可注射() 导出类MyService{ 路径:字符串; 建造师( 私人otherService:otherService, ) { } getAll(){ 返回this.httpClient.post(this.PATH,request); //仅供参考:这仅用于示例目的 } @NgModule({ 声

我有一个带有参数
PATH
的服务,我需要在模块的提供者中设置此服务时设置此参数

当我将此服务注入此模块的某个组件并调用该服务的任何方法时,参数
PATH
将已设置

我跟着这个医生,但对我没有帮助


@可注射()
导出类MyService{
路径:字符串;
建造师(
私人otherService:otherService,
) {
}
getAll(){
返回this.httpClient.post(this.PATH,request);
//仅供参考:这仅用于示例目的
}
@NgModule({
声明:[
某些组件,
],
进口:[
公共模块,
],
供应商:[
MyService/<---我需要在这里设置参数路径
]
})
导出类MyModule{

导出类MyComponent实现OnInit{
建造师(
私有myService:myService
) { }
恩戈尼尼特(){

this.myService.getAll();//您需要的是工厂提供程序。它允许您参数化服务创建

至少有几种方法可以做到这一点,使用某些路径参数最简单的方法可能是如下所示为其创建InjectionToken:

// in service you add token definition:
export const PATH = new InjectionToken<string>('PATH');

@Injectable()
export class MyService {
  constructor(
    @Inject(PATH) private path: string,
  ) {
  }
  ...
}

有四个选项。可能更多,但我想这些是最常用的一次,根据您的用例,您可以决定您需要什么

1.使用 使用这样的令牌,您可以使用
AppModule
的providers数组向服务中注入常量:

首先创建注入令牌文件:

path.token.ts

export const PATH = new InjectionToken<string>('PathToSomethingDescription');
@NgModule({
  // ...
  providers: [
    { provide: PATH, useValue: 'https://foo-bar' } 
  ]
})
export class AppModule {}
export class MyService {
  constructor(@Inject(PATH) path: string) {}
}
export class MyService {
  PATH: string = environment.path;
}
@Injectable({
  providedIn: 'root',
  useFactory: () => new MyService('https://foo-bar'),
})
export class MyService {
  constructor(readonly PATH: string) {}
}
@NgModule({
  providers: [
    // ...
    { provide: MyService, useFactory: () => new MyService('https://foo-bar') }
  ]
})
export class RandomFeatureModule {}
export class MyService {
  constructor(readonly PATH?: string) {}
}
my.service.ts

export const PATH = new InjectionToken<string>('PathToSomethingDescription');
@NgModule({
  // ...
  providers: [
    { provide: PATH, useValue: 'https://foo-bar' } 
  ]
})
export class AppModule {}
export class MyService {
  constructor(@Inject(PATH) path: string) {}
}
export class MyService {
  PATH: string = environment.path;
}
@Injectable({
  providedIn: 'root',
  useFactory: () => new MyService('https://foo-bar'),
})
export class MyService {
  constructor(readonly PATH: string) {}
}
@NgModule({
  providers: [
    // ...
    { provide: MyService, useFactory: () => new MyService('https://foo-bar') }
  ]
})
export class RandomFeatureModule {}
export class MyService {
  constructor(readonly PATH?: string) {}
}
然后,您可以从服务内部使用
this.path
,它将具有提供者定义中提供的值。如果希望在某些模块中有所不同,您还可以覆盖此
InjectionToken


2.使用angular cli中的环境文件 可以使用环境文件插入常量:

环境。ts

export const environment = {
  // ...
  path: 'https://foo-bar'
};
my.service.ts

export const PATH = new InjectionToken<string>('PathToSomethingDescription');
@NgModule({
  // ...
  providers: [
    { provide: PATH, useValue: 'https://foo-bar' } 
  ]
})
export class AppModule {}
export class MyService {
  constructor(@Inject(PATH) path: string) {}
}
export class MyService {
  PATH: string = environment.path;
}
@Injectable({
  providedIn: 'root',
  useFactory: () => new MyService('https://foo-bar'),
})
export class MyService {
  constructor(readonly PATH: string) {}
}
@NgModule({
  providers: [
    // ...
    { provide: MyService, useFactory: () => new MyService('https://foo-bar') }
  ]
})
export class RandomFeatureModule {}
export class MyService {
  constructor(readonly PATH?: string) {}
}

3.创建提供者 我的服务。ts

export const PATH = new InjectionToken<string>('PathToSomethingDescription');
@NgModule({
  // ...
  providers: [
    { provide: PATH, useValue: 'https://foo-bar' } 
  ]
})
export class AppModule {}
export class MyService {
  constructor(@Inject(PATH) path: string) {}
}
export class MyService {
  PATH: string = environment.path;
}
@Injectable({
  providedIn: 'root',
  useFactory: () => new MyService('https://foo-bar'),
})
export class MyService {
  constructor(readonly PATH: string) {}
}
@NgModule({
  providers: [
    // ...
    { provide: MyService, useFactory: () => new MyService('https://foo-bar') }
  ]
})
export class RandomFeatureModule {}
export class MyService {
  constructor(readonly PATH?: string) {}
}

4.跨模块使用
useFactory
创建提供程序 如果您需要跨模块使用不同的值,您不应该这样做,而是从功能模块中的
@Injectable
添加useFactory:

随机特征.module.ts

export const PATH = new InjectionToken<string>('PathToSomethingDescription');
@NgModule({
  // ...
  providers: [
    { provide: PATH, useValue: 'https://foo-bar' } 
  ]
})
export class AppModule {}
export class MyService {
  constructor(@Inject(PATH) path: string) {}
}
export class MyService {
  PATH: string = environment.path;
}
@Injectable({
  providedIn: 'root',
  useFactory: () => new MyService('https://foo-bar'),
})
export class MyService {
  constructor(readonly PATH: string) {}
}
@NgModule({
  providers: [
    // ...
    { provide: MyService, useFactory: () => new MyService('https://foo-bar') }
  ]
})
export class RandomFeatureModule {}
export class MyService {
  constructor(readonly PATH?: string) {}
}
我的服务。ts

export const PATH = new InjectionToken<string>('PathToSomethingDescription');
@NgModule({
  // ...
  providers: [
    { provide: PATH, useValue: 'https://foo-bar' } 
  ]
})
export class AppModule {}
export class MyService {
  constructor(@Inject(PATH) path: string) {}
}
export class MyService {
  PATH: string = environment.path;
}
@Injectable({
  providedIn: 'root',
  useFactory: () => new MyService('https://foo-bar'),
})
export class MyService {
  constructor(readonly PATH: string) {}
}
@NgModule({
  providers: [
    // ...
    { provide: MyService, useFactory: () => new MyService('https://foo-bar') }
  ]
})
export class RandomFeatureModule {}
export class MyService {
  constructor(readonly PATH?: string) {}
}

然后通常将其注入到需要的任何位置。但请注意,您将获得同一服务的不同实例。这可能会导致意外行为

如果存在许多依赖项,并且只需要静态提供一个依赖项,则此解决方案并不理想。使用InjectionToken要简单得多。如果需要,工厂会更好例如,在您创建的服务被注入某个地方之前,请对其执行一些附加操作。“请”更“谨慎地”使用“引号”。还有什么“对我没有帮助”的意思-您从文档中学到了什么,您还想知道什么?一个要研究的选项可能是静态方法,因此您应该包括例如
MyService.withPath(…)
提供程序中的
。如果您不为创建导出函数,AoT将投诉factories@Xesenix过去确实如此,据我所知,现在不是了:)