在两个版本中拆分 Angular CLI 应用程序

Joh*_*srk 5 configuration split angular-cli angular

我有一个使用 CLI 在 Angular 7 上构建的角度解决方案。该应用程序有 2 个区域。它们没有以任何方式连接,但它们使用相同的代码库。

现在我可以去http://localhost:4200/app1,或http://localhost:4200/app2。但它们运行在同一个实例上。我想修改我的配置,这样我就可以写一些类似的东西

ng build --app=app1
Run Code Online (Sandbox Code Playgroud)

并获得独立的解决方案。有什么建议,我能做什么?

下面是我的 angular.json 文件。如您所见,我已经为开发、登台和生产做好了准备。这意味着此刻,我可以写

ng build --build-optimizer --configuration=staging
Run Code Online (Sandbox Code Playgroud)

并为登台环境构建一个版本。

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "vhTogo": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/vhTogo",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/assets",
              "src/manifest.json"
            ],
            "styles": [
              "src/styles/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputPath": "dist/production",
              "vendorChunk": true,
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "buildOptimizer": true,
              "serviceWorker": true
            },
            "staging": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.staging.ts"
                }
              ],
              "outputPath": "dist/staging",
              "vendorChunk": true,
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "buildOptimizer": true,
              "serviceWorker": true
            },
            "development": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.development.ts"
                }
              ],
              "outputPath": "dist/development",
              "vendorChunk": true,
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "buildOptimizer": true,
              "serviceWorker": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "vhTogo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "vhTogo:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "vhTogo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts", 
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles/styles.scss"
            ],
            "scripts": [],
            "assets": [
              "src/assets",
              "src/manifest.json"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "vhTogo-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "vhTogo:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "vhTogo:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "vhTogo",
  "schematics": {
    "@schematics/angular:component": {
      "styleext": "scss"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Vik*_*iya 3

使用cli您可以在同一代码库(存储库)中生成多个角度项目,并可以单独构建、开发或服务它们。通过这种方式,您可以在前端级别实施微服务架构。如果您在同一域下有不同的产品并且您希望通过不同的路由将它们分开,那么这是最好的方法。

以下是帮助您构建这种架构的一些步骤

  1. 生成多个角度应用程序:ng generate application <project-name> 单击此处获取信息

  2. 分别构建和服务它们:ng build project-nameng serve project-name

  3. 配置路由:要使此路由成为可能,您必须配置nginx代理服务器。

您必须在 nginx 脚本中使用位置块来从不同的项目提供服务,以下脚本仅用于演示,请参阅文档以获取完整的实现。

server {
    listen 4200;

    location / {
        proxy_pass http://localhost:4201;  # your root project
    }

    location /app1 {
        proxy_pass http://localhost:4202;  # your app1 project
    }

    location /app2 {
        proxy_pass http://localhost:4203;  # your app2 project
    }

}
Run Code Online (Sandbox Code Playgroud)

对于生产,您可以在位置块中提供相应项目的 index.html 文件