在 Angular 节点包中使用环境变量 [英] Using Environment Variables in Angular Node Package

查看:41
本文介绍了在 Angular 节点包中使用环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们创建的日志服务创建一个 angular 4 节点包.是否可以使用 angular cli 环境变量来设置服务的默认路径?如果是这样,应该如何定义环境文件的路径?

I am creating an angular 4 node package for a logging service that we have created. Is it possible to use the angular cli environment variables to set the default path for the service? If so, how should the path be defined for the environment file?

我正在按照以下说明创建节点包:https://medium.com/@cyrilletuzi/how-to-build-and-publish-an-angular-module-7ad19c0b4464

I am following these instructions for creating the node package: https://medium.com/@cyrilletuzi/how-to-build-and-publish-an-angular-module-7ad19c0b4464

我按照以下示例设置环境变量:http://tattoocoder.com/angular-cli-using-the-environment-选项/

I followed these examples for setting up environment variables: http://tattoocoder.com/angular-cli-using-the-environment-option/

https://medium.com/beautiful-angular/angular-2-and-environment-variables-59c57ba643be

推荐答案

你可以使用 forRoot 方法此处.

在外部 Angular 库中修改主要导出的模块以具有以下结构:

Inside of your external Angular library modify the main exported module to have this structure:

export class MyLibraryModule {
  static forRoot(config: configData): ModuleWithProviders {
    return {
      ngModule: MyLibraryModule,
      providers: [
        {provide: AppConfigModel, useValue: config }
      ]
    };
  }

  constructor(@Optional() config: AppConfigModel) {
    if (config) { /* Save config data */ }
  }

 }

 export class AppConfigModel {
    defaultPath: string;
 }

将此模块导入应用程序时,请使用 forRoot 静态方法并传入配置数据.

When you import this module into your application, use the forRoot static method and pass in the config data.

imports: [
    MyLibraryModule.forRoot({ defaultPath: environment.defaultPath }),
 ]

附带说明,我使用这个 library 来创建我的 Angularnpm 模块.它大大简化了流程.

On a side note, I use this library for creating my Angular npm modules. It simplifies the process quite a bit.

这篇关于在 Angular 节点包中使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆