【问题标题】:Environment issue in Angular when try to build尝试构建时Angular中的环境问题
【发布时间】:2020-08-20 03:46:56
【问题描述】:

当我尝试构建一个 Angular 项目时,我遇到了这个问题:

文件 '/angular/src/environments/environment.ts' 不是模块

我像这样导入文件:

import { environment } from '../../environments/environment';

我的tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

在我的环境文件夹中,我有文件:environment.tsenvironment.prod.tsenvironment.dev.ts

我使用 NODE_VERSION=10,NG_CLI_VERSION=8

我的 angular.json,build.configurations for dev :

"dev": {
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true,
          "serviceWorker": true
        }

构建命令:"build": "ng build --configuration=dev --aot",

请帮助我。提前致谢!

【问题讨论】:

  • 你用什么命令来编译代码?另外,您是否考虑过升级到 Angular 9 或 10?
  • 命令:ng build --configuration=dev --aot。我暂时不能升级
  • 那么我们需要 angular.json
  • 在某些时候,cli 开始要求在 tsconfig 中包含所有候选环境替换(或 angular.json 无法记住),以使其正常工作。仅在 env 目录中有 environment.xxx.ts 是不够的。
  • @Antoniossss 我可以确认情况并非如此。至少不适用于 Angular 9...

标签: angular typescript npm angular8 angular-cli-v8


【解决方案1】:

Angular 默认只配置angular.json 生产环境来替换environement.ts 文件。它使用angular.json的这一部分。

"configurations": {
   "production":{
      "fileReplacements":[
         {
            "replace":"src/environments/environment.ts",
            "with":"src/environments/environment.prod.ts"
         }
      ],
      "rest": ...
   }
}

开发人员不能使用environment.dev.ts,您还需要将fileReplacements 部分添加到开发人员构建中。

"fileReplacements":[
  {
    "replace":"src/environments/environment.ts",
    "with":"src/environments/environment.dev.ts"
  }
]

或者将你的开发配置放到非后固定的配置中。

【讨论】:

    猜你喜欢
    • 2020-11-07
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多