0

当我尝试构建一个 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.ts, environment.prod.ts, environment.dev.ts.

我使用 NODE_VERSION=10, NG_CLI_VERSION=8

我的 angular.json,build.configurationsfor 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",

请帮我。提前致谢 !

4

1 回答 1

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-05-05T14:13:21.210 回答