Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何将服务注入到另一个服务中,并在导出的常量或接口中使用它?_Angular_Typescript_Dependency Injection_Typescript Typings_Angular Components - Fatal编程技术网

Angular 如何将服务注入到另一个服务中,并在导出的常量或接口中使用它?

Angular 如何将服务注入到另一个服务中,并在导出的常量或接口中使用它?,angular,typescript,dependency-injection,typescript-typings,angular-components,Angular,Typescript,Dependency Injection,Typescript Typings,Angular Components,我想将一个服务注入到另一个服务中,我想用它来翻译导出常量中的字符串 我的代码目前看起来像这样(我在这里简化了它) 但是,由于在组件外部未检测到注入的服务,因此出现此错误: 错误:未捕获(承诺中):TypeError:无法读取未定义的属性“injectedService” 这个问题最干净的解决方案是什么?您只需在服务中创建一个getSeries方法,该方法将返回常量的值此将在MyService类中定义,因此没有错误 @Injectable({ providedIn: 'root' }) exp

我想将一个服务注入到另一个服务中,我想用它来翻译导出常量中的字符串

我的代码目前看起来像这样(我在这里简化了它)

但是,由于在组件外部未检测到注入的服务,因此出现此错误:

错误:未捕获(承诺中):TypeError:无法读取未定义的属性“injectedService”


这个问题最干净的解决方案是什么?

您只需在服务中创建一个
getSeries
方法,该方法将返回常量的值<代码>此将在
MyService
类中定义,因此没有错误

@Injectable({
  providedIn: 'root'
})
export class MyService {

  constructor(private httpClient: HttpClient, private injectedService: InjectedService) {}

  // Methods..

  getSeries(): Series[] {
    return [
      this.injectedService.translate(Series.prop_1),
      this.injectedService.translate(Series.prop_1),
      this.injectedService.translate(Series.prop_1),
    ];
  }
}

export enum Series {
  prop_1 = 'String_1',
    prop_2 = 'String_2',
    prop_3 = 'String_3',
}

您需要调用
getSeries()
方法,而不是
ALL\u SERIES

您只需在服务中创建一个
getSeries
方法,该方法将返回常量的值<代码>此将在
MyService
类中定义,因此没有错误

@Injectable({
  providedIn: 'root'
})
export class MyService {

  constructor(private httpClient: HttpClient, private injectedService: InjectedService) {}

  // Methods..

  getSeries(): Series[] {
    return [
      this.injectedService.translate(Series.prop_1),
      this.injectedService.translate(Series.prop_1),
      this.injectedService.translate(Series.prop_1),
    ];
  }
}

export enum Series {
  prop_1 = 'String_1',
    prop_2 = 'String_2',
    prop_3 = 'String_3',
}

您需要调用
getSeries()
方法,而不是
ALL\u SERIES

构造函数中没有像httpClient那样提到injectedService的类型,如果要提到injectedService的类型,那么angular将知道使用什么类型DI@manishgdev我很抱歉,我把它忘在简化版里了。仍然不起作用。如果要在服务外部使用服务,则在另一个服务内部注入服务是没有意义的。这正是我的问题。我如何继续?这可能有助于在构造函数中没有像在httpClient中提到的那样提到injectedService的类型,如果提到injectedService的类型,那么angular将知道使用什么类型DI@manishgdev对不起,我的简化版忘了。仍然不起作用。如果要在服务外部使用服务,则在另一个服务内部注入服务是没有意义的。这正是我的问题。我怎样才能继续?也许这会有帮助