angular - 注入服务以填充 environment.ts 文件中的值
问题描述
我有一项angular
服务 ( clientAppSettings.service.ts
)。它从后端获取配置值,这是一个 json 文件 ( appsettings.json
)
我需要注入角度服务,填写environment.ts
文件中的值。因此,instrumentaionKey
应该从服务 (clientAppSettings.service.ts) 中检索。任何指南表示赞赏。
这是environmen.ts
文件:
export const environment = {
production: false,
appInsights: {
instrumentationKey: 'some-key'
}
};
角度服务clientAppSettings.service.ts
::
@Injectable()
export class ClientAppSettingService {
appUrl: string = "";
constructor(private http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
this.appUrl = baseUrl;
}
getConfig() {
return this.http.get<ClientAppSettings>(this.appUrl + 'api/ClientAppSettings');
}
}
后端控制器:
namespace WebApp.Controllers
{
[Route("api/[controller]")]
public class ClientAppSettingsController : Controller
{
private readonly AppSettings _clientAppSettings;
public ClientAppSettingsController(IOptions<AppSettings> appSettings)
{
_clientAppSettings = appSettings.Value;
}
[HttpGet("[action]")]
public AppSettings Get()
{
return _clientAppSettings;
}
}
}
和appsetting.json
:
{
"AppConfig": {
"ApplicationInsights": {
"InstrumentationKey": "some-key"
}
}
解决方案
您无法在运行时更新 env 文件,它在构建时有角度使用。只需将设置保存在您的服务中并从那里提取它们。
推荐阅读
- javascript - 使用可选值过滤集合
- laravel - 如何在没有密钥的情况下删除 Laravel 中的 hasMany 关系
- javascript - 在检查器中可视化 css 网格计算状态
- outlook - 尝试使用 rdoDefaultFolders.olFolderAllItemsSearch 调用 GetDefaultFolder 时,兑换会引发错误
- laravel-5.6 - 页面未正确重定向 Laravel 5.6
- python - 假设我将数字作为字符串或十六进制或整数,并且我想转换为整数。我正在寻找更通用的形式
- python - Python:CountVectorizer 忽略一个字母单词“I”
- salt-stack - 使用在支柱中定义的角色定位奴才
- ruby - 将字符串与Ruby中文件中的字符串进行比较
- java - java.lang.IllegalArgumentException:不是 Spring Boot 应用程序中的托管类型