Angular 8导入服务到接口/模块

问题描述 投票:0回答:1

我有一个服务,在config.serivce.ts中称为ConfigService,我需要获取一个函数(getClientID),该函数返回一个值并将该值导入到模块文件中:appAuth.module.ts

我需要知道如何从这样的文件中的服务函数注入值?

例如在我的appAuth.module.ts中:

 @NgModule({
   imports: [
    CommonModule,
    AuthModule.forRoot<any>({
     adapter: getClientID(),
    })],
   providers: [],
   declarations: []
    })
   export class AppAuthModule { }
angular angular8 angular-services angular-dependency-injection
1个回答
1
投票

无论何时需要getClientId()的结果,都应该注入ConfigService并调用getClientId(),而不是从环境中读取它。

据我所知,您的环境文件不应依赖任何内容。相反,您的服务,组件等应取决于环境文件。

这里是Angular documentation on dependency injection中的一个例子。它显示了如何(在这种情况下)将UserServiceLoggerService注入UserContextService

@Injectable({
  providedIn: 'root'
})
export class UserContextService {
  constructor(private userService: UserService, private loggerService: LoggerService) {
  }
}

请参阅Angular documentation以获取有关如何使用环境文件的示例。

© www.soinside.com 2019 - 2024. All rights reserved.