Angular 9:文件替换不起作用

Sta*_*sev 7 angular

fileReplacements如果它们出现在不替换文件assets。此代码不适用于 Angular 9,但适用于版本 8:

"assets": [
  "src/web.config"
]
...
"fileReplacements": [
  {
    "replace": "src/web.config",
    "with": "src/configs/web.stage.config"
  }
]
Run Code Online (Sandbox Code Playgroud)

如何修复?

Kur*_*ton 13

您应该使用 assets 属性而不是文件替换来复制资产。

这是我的angular.json文件的简化,仅显示configurations.

{  
  "projects": {
    "my-project": {      
      "architect": {
        "build": {          
          "configurations": {
            "staging": {
              "assets": [
                { "glob": "**/*", "input": "src/assets/", "output": "/assets/" },
                { "glob": "web.config", "input": "src/environments/staging/", "output": "/" }
              ],              
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.staging.ts"
                }
              ]
            },
            "production": {
              "assets": [
                { "glob": "**/*", "input": "src/assets/", "output": "/assets/" },
                { "glob": "web.config", "input": "src/environments/prod/", "output": "/" }
              ],            
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            }
          }
        }
      }
    }
  }
}

Run Code Online (Sandbox Code Playgroud)

  • 谢谢。这有效......但仅在构建时有效,而不是在服务时有效:-( (2认同)