Angular 8 配置文件

本文介绍如何在Angular项目中配置开发(dev)与生产(prod)环境,通过修改angular.json文件实现不同环境变量的切换,确保应用程序在不同阶段运行正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目在开发时与发布后的配置文件(比如url)是有区别的,因此需要根据不同的场景使用不同的配置。

环境

>ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 7.3.3
Node: 10.13.0
OS: win32 x64
Angular: 7.2.6
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.13.3
@angular-devkit/build-angular     0.13.3
@angular-devkit/build-optimizer   0.13.3
@angular-devkit/build-webpack     0.13.3
@angular-devkit/core              7.3.3
@angular-devkit/schematics        7.3.3
@angular/cli                      7.3.3
@ngtools/webpack                  7.3.3
@schematics/angular               7.3.3
@schematics/update                0.13.3
rxjs                              6.3.3
typescript                        3.2.4
webpack                           4.29.0

思路

分别配置两套 Config,devprod,之后在发布网站时选择不同的配置项进行发布即可。

配置

打开 angular.json -> projects -> 项目名 -> architect -> build -> configurations -> production。复制该配置以设置更多的配置项,如下:

"configurations": {
  "prod": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ],
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": false,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": false,
    "buildOptimizer": true,
    "budgets": [
      {
        "type": "initial",
        "maximumWarning": "2mb",
        "maximumError": "5mb"
      }
    ]
  },
  "dev": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.dev.ts"
      }
    ],
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": false,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": false,
    "buildOptimizer": true,
    "budgets": [
      {
        "type": "initial",
        "maximumWarning": "2mb",
        "maximumError": "5mb"
      }
    ]
  }
}

上面的配置中,配置了 2 个配置项,分别是 devprod
dev 与 prod 是配置中节点的名字,随后 build 时需要用到他们。

编辑 dev 与 prod 环境文件

src -> environments 中复制 environments.prod.ts, 粘贴后更改名字为 environments.dev.ts
environment.ts 文件内容:

export const environment = {
  production: false,
  envType: 'test'
};

environment.dev.ts 文件内容:

export const environment = {
  production: false,
  envType: 'dev'
};

environment.prod.ts 文件内容:

export const environment = {
  production: true,
  envType: 'prod'
};

此时的 environments 文件夹应该是这样的
├── environment.dev.ts
├── environment.prod.ts
└── environment.ts

使用配置项

app.component.html:

<h2>{{envType}}</h2>

app.component.ts:

import { Component, OnInit } from '@angular/core';
import { environment } from 'src/environments/environment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  public envType: string;

  constructor() { }

  ngOnInit(): void {
    this.envType = environment.envType;
  }
}

为了方便测试,打开 angular.json -> projects -> 项目名 -> architect -> serve -> configurations 修改如下:

"configurations": {
  "prod": {
    "browserTarget": "ng-practice:build:prod"
  },
  "dev": {
    "browserTarget": "ng-practice:build:dev"
  }
}

分别执行: ng s -c=devng s -c=prodng s,会看到不同的结果:dev, prod, test

在发布时,需要将命令稍作修改: ng b --prod -c=prod

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值