简体   繁体   English

如何从 angular 中的 app.module 调用服务内的方法并使用 Imports 中的值

[英]How to call a method inside a service from app.module in angular and use the values in Imports

I'm moving all the environment related data from angular to external api call so that it can be build once and deployed in multiple environments.我正在将所有与环境相关的数据从 angular 移动到外部 api 调用,以便它可以构建一次并部署在多个环境中。 For NGX Logger it is imported in app.module.ts and the configs for level and serverLogLevel is fetched from environment.ts currently since it varies based on evironments.对于 NGX Logger,它是在app.module.ts中导入的,而levelserverLogLevel的配置目前是从environment.ts中获取的,因为它根据环境而有所不同。

This is the current NGXLogger config这是当前的 NGXLogger 配置

imports:[
LoggerModule.forRoot({
level: NgxLoggerLevel[env.consolelvl],
serverLogLevel: NgxLogerLevel[env.serverlvl]
})
]

I want these env variables to be fetched from api call.我希望从 api 调用中获取这些环境变量。 How can this be achieved.这怎么可能实现。

You can set a default value for your levels您可以为级别设置默认值

And later on when you execute your API call you can update the logger config with partialUpdateConfig稍后当您执行 API 调用时,您可以使用partialUpdateConfig更新记录器配置

Attention, this means the logs called before your API call will use the default level注意,这意味着在您调用 API 之前调用的日志将使用默认级别

PS: This requires 5.0.11, if you have an older version you need to use updateConfig PS:这个需要5.0.11,如果你有旧版本需要使用updateConfig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在app.module中的服务内部调用方法-Angular - How to call a method inside of a service in app.module - Angular Angular将HttpClient从app.module传递到另一个模块到服务 - Angular Pass HttpClient from app.module to another module to a service 通过从后端获取数据来设置从 app.module 导入 - Set imports from app.module by getting data from backend App.Module 导入在 Ionic 页面上不起作用 - App.Module imports not working on Ionic pages 在main.ts和app.module中使用相同的服务实例化(到处都是单个) - Use the same service instantiation from main.ts and in app.module (singleton everywhere) 如何在 Angular 的 app.module 中使用使用条件导出的 class? - How can I use a class that use conditional export in Angular's app.module? 如何使用服务在app.module上一次初始化变量,然后在其他组件中使用它? - How to initialize a variable once on the app.module using a service and use it later in other components? 从服务文件获取值到app.module的最佳方法 - Best way to get a value from service file to app.module 如何在Angular 5中的app.module中读取appsettings文件? - How to read appsettings file in app.module in Angular 5? 如何将angular 2 app.module转换为es6 - how can I convert angular 2 app.module into es6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM