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

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

问题描述

我正在为我们创建的日志记录服务创建一个有角度的4节点程序包.是否可以使用cli 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/如何构建和发布一个角度模块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 -option/

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

推荐答案

您可以使用此处.

You can do that using the forRoot methodology explained in the Angular docs here.

在外部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 }),
 ]

在旁注中,我使用此来创建我的Angular npm模块.它大大简化了该过程.

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天全站免登陆