当前位置: 代码网 > it编程>前端脚本>AngularJs > 详解如何在Angular中使用环境变量

详解如何在Angular中使用环境变量

2024年05月15日 AngularJs 我要评论
简介如果你正在构建一个使用 api 的应用程序,你会想在开发过程中使用测试环境的 api 密钥,而在生产环境中使用生产环境的 api 密钥。在 angular 中,你可以通过 environment.

简介

如果你正在构建一个使用 api 的应用程序,你会想在开发过程中使用测试环境的 api 密钥,而在生产环境中使用生产环境的 api 密钥。在 angular 中,你可以通过 environment.ts 文件创建环境变量。

在本教程中,你将学习如何在 angular 中使用环境变量。

先决条件

如果你想跟着本文操作,你需要:

  • 一个用于 node.js 的本地开发环境。请参考《如何安装 node.js 并创建本地开发环境》。

本教程已经验证过可以在 node v16.2.0、npm v7.15.1 和 @angular/core v12.0.3 下运行。

检测环境

angular cli 项目已经使用了一个 production 环境变量,在生产环境下启用生产模式:

// ...

if (environment.production) {
  enableprodmode();
}

angular 还为我们提供了一个名为 isdevmode 的实用函数,可以用来检查应用程序是否在开发模式下运行:

import { component, oninit, isdevmode } from '@angular/core';

@component({ ... })
export class appcomponent implements oninit {
  ngoninit() {
    if (isdevmode()) {
      console.log('development!');
    } else {
      console.log('production!');
    }
  }
}

这个示例代码将在开发模式下记录消息 'development!',在生产模式下记录消息 'production!'

添加开发和生产变量

你还会注意到,默认情况下,在 /src/environment 文件夹中有一个用于开发环境和一个用于生产环境的环境文件。

假设我们想要根据是否处于开发或生产模式来使用不同的密钥:

对于 environment.ts 中的开发设置:

export const environment = {
  production: false,
  apikey: 'devkey'
};

对于 environment.prod.ts 中的生产设置:

export const environment = {
  production: true,
  apikey: 'prodkey'
};

在我们的组件中,我们只需要这样做就可以访问变量:

import { component } from '@angular/core';
import { environment } from '../environments/environment';

angular 会负责切换正确的环境文件。

使用以下命令运行开发模式:

ng serve

apikey 变量将解析为 devkey

使用以下命令运行生产模式:

ng serve --configuration=production

apikey 变量将解析为 prodkey

添加暂存变量

通过在 angular.json 文件的 configurations 字段中添加新条目,可以在 angular cli 项目中添加新的环境。

让我们基于生产使用的配置添加一个暂存环境:

{
  // ...
  "projects": {
    "angular-environment-example": {
      // ...
      "prefix": "app",
        "build": {
          // ...
          "configurations": {
            "staging": {
              // ...
              "filereplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.stage.ts"
                }
              ],
              // ...
            },
            // ...
          },
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "staging": {
              "browsertarget": "angular-environment-example:build:staging"
            },
          }
        },
      }
    }
  }
}

现在我们可以添加一个暂存环境文件:

export const environment = {
  production: true,
  apikey: 'stagingkey'
};

使用以下命令运行开发模式:

ng serve --configuration=staging

apikey 变量将解析为 stagingkey

结论

在本教程中,你学习了如何在 angular 中使用环境变量。

如果你想了解更多关于 angular 的知识,请查看我们的 angular 主题页面,了解练习和编程项目。

以上就是详解如何在angular中使用环境变量的详细内容,更多关于angular环境变量的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com