如何在同一端口上运行两个角度应用程序

发布于 2025-01-16 01:05:42 字数 6026 浏览 1 评论 0原文

我正在尝试在同一端口上运行两个角度应用程序,为了进行开发,我使用http://localhost:4200。有谁知道如何在同一端口上运行两个角度应用程序?有什么简单的解决办法吗?如果有人知道如何做到这一点,您能告诉我如何将其添加到我的应用程序中吗?谢谢你!

这是我的角度json,它可以帮助解决这个问题:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "app1": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "projects/app1",
      "sourceRoot": "projects/app1/src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/app1",
            "index": "projects/app1/src/index.html",
            "main": "projects/app1/src/main.ts",
            "polyfills": "projects/app1/src/polyfills.ts",
            "tsConfig": "projects/app1/tsconfig.app.json",
            "assets": [
              "projects/app1/src/favicon.ico",
              "projects/app1/src/assets"
            ],
            "styles": [
              "projects/app1/src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "projects/app1/src/environments/environment.ts",
                  "with": "projects/app1/src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "app1:build:production"
            },
            "development": {
              "browserTarget": "app1:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "app1:build"
          }
        },
      }
    },

    "app2": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "projects/app2",
      "sourceRoot": "projects/app2/src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/app2",
            "index": "projects/app2/src/index.html",
            "main": "projects/app2/src/main.ts",
            "polyfills": "projects/app2/src/polyfills.ts",
            "tsConfig": "projects/app2/tsconfig.app.json",
            "assets": [
              "projects/app2/src/favicon.ico",
              "projects/app2/src/assets"
            ],
            "styles": [
              "projects/app2/src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "projects/app2/src/environments/environment.ts",
                  "with": "projects/app2/src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "app2:build:production"
            },
            "development": {
              "browserTarget": "app2:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "app2:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/app2/src/test.ts",
            "polyfills": "projects/app2/src/polyfills.ts",
            "tsConfig": "projects/app2/tsconfig.spec.json",
            "karmaConfig": "projects/app2/karma.conf.js",
            "assets": [
              "projects/app2/src/favicon.ico",
              "projects/app2/src/assets"
            ],
            "styles": [
              "projects/app2/src/styles.css"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "app1"
}

I am trying to run two angular apps on the same port, for developping i am using http://localhost:4200. Does anyone knows how to run two angular apps on the same port? Is there any easy solution for it? If anyone knows how to do that, could you tell me how to add that to my app. Thank you!

This is my angular json, it could help to solve this problem:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "app1": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "projects/app1",
      "sourceRoot": "projects/app1/src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/app1",
            "index": "projects/app1/src/index.html",
            "main": "projects/app1/src/main.ts",
            "polyfills": "projects/app1/src/polyfills.ts",
            "tsConfig": "projects/app1/tsconfig.app.json",
            "assets": [
              "projects/app1/src/favicon.ico",
              "projects/app1/src/assets"
            ],
            "styles": [
              "projects/app1/src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "projects/app1/src/environments/environment.ts",
                  "with": "projects/app1/src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "app1:build:production"
            },
            "development": {
              "browserTarget": "app1:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "app1:build"
          }
        },
      }
    },

    "app2": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "projects/app2",
      "sourceRoot": "projects/app2/src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/app2",
            "index": "projects/app2/src/index.html",
            "main": "projects/app2/src/main.ts",
            "polyfills": "projects/app2/src/polyfills.ts",
            "tsConfig": "projects/app2/tsconfig.app.json",
            "assets": [
              "projects/app2/src/favicon.ico",
              "projects/app2/src/assets"
            ],
            "styles": [
              "projects/app2/src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "projects/app2/src/environments/environment.ts",
                  "with": "projects/app2/src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "app2:build:production"
            },
            "development": {
              "browserTarget": "app2:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "app2:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/app2/src/test.ts",
            "polyfills": "projects/app2/src/polyfills.ts",
            "tsConfig": "projects/app2/tsconfig.spec.json",
            "karmaConfig": "projects/app2/karma.conf.js",
            "assets": [
              "projects/app2/src/favicon.ico",
              "projects/app2/src/assets"
            ],
            "styles": [
              "projects/app2/src/styles.css"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "app1"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天气好吗我好吗 2025-01-23 01:05:42

根据您的最终目标,这篇文章应该可以回答您的问题。您将需要构建并托管这两个应用程序。人们需要使用不同的 base-href 选项。 我们可以在同一端口下托管两个 Angular 项目

Depending on your ultimate objective, this post should answer your question. You will need to build and host both applications. One will need to use a different base-href option. Can we host two angular projects under same port

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文