Angular 9:文件替换无效

9

如果assets中存在文件,则Angular的fileReplacements不会替换文件。 这段代码在Angular 9中不起作用,但在8版本中有效:

"assets": [
  "src/web.config"
]
...
"fileReplacements": [
  {
    "replace": "src/web.config",
    "with": "src/configs/web.stage.config"
  }
]

如何修复?

2个回答

15

你应该使用资源属性来复制资产,而不是文件替换。

这是我的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"
                }
              ]
            }
          }
        }
      }
    }
  }
}


2
谢谢。这个可以用...但只有在构建时有效,而不是在服务时 :-( - Ulises 2010
请看这里:https://github.com/angular/angular-cli/issues/16779#issuecomment-579183693 - Nickolaus
发现了一篇很棒的文章:https://medium.com/@pedrorolo/angular-setting-angular-json-b485dedc29d7 - undefined

0
我在使用 Angular 9 时遇到了相同的问题,降级 "@angular-devkit/build-angular" 版本从 "^0.1000.3" 到 "^0.900.7" 就解决了问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接