首页 > 解决方案 > 从角度环境中排除文件

问题描述

我正在构建一个安装在不同客户端中的应用程序。为此,我使用不同的环境,例如

环境.costumer1.ts 环境.costumer2.ts

事实是一些客户使用服务工作者,而另一个没有,所以我创建了两个不同的 app.module.ts

app.module.ts
app.module-without-service-worker.ts

因此,当客户要求我提供没有 Service Worker 的应用程序时(我使用了一个我不想显示谁不想要它的通知服务),我将它放在 angular.json 中的 fileReplacements

{   
  "replace": "src / app / app.module.ts",
  "with": "src / app / app.module-without-service-worker.ts" 
}

不好的是,在构建主应用程序时,我收到以下错误:

错误:在 D:/developments/Patient Portal/patient-portal-demo/pp demo front/src/app/app.component.ts 中键入 AppComponent 是 2 个模块声明的一部分:D:/development/Patient 中的 AppModule Portal/patient-portal-demo/pp demo front/src/app/app.module.ts和AppModule在D:/developments/Patient Portal/patient-portal-demo/pp demo front/src/app/app.module-没有-service-worker.ts!

有没有办法从环境中排除文件?

标签: angularangular-cli

解决方案


在为自己寻找答案时,我找到了您的帖子。

我尝试了基于构建配置加载不同组件(和不同路由配置)的方法,它对我有用。

在 Angular 9 中工作。

这是我的 angular.json:

"build": {
...
"configurations": {
   "custom": {
     "fileReplacements": [
       {
         "replace": "projects/my-app/src/app/app.module.ts",
         "with": "projects/my-app/src/app/app.module-custom.ts"
       }
     ]
   },
...
},
...
}
...
"serve": {
  ...
    "configurations": {
      ...
      "app": {
        "browserTarget": "rest-generator-app:build:custom"
      }
    }
  }

然后当我提供我的应用程序时:

ng serve my-app --port=3000 -o --configuration=custom

推荐阅读