首页 > 解决方案 > Angular:在 localhost 上使用 npm start 在自定义上下文根上提供应用程序

问题描述

我有一个 Angular 应用程序,它使用以下配置在端口 4300 上启动:

"scripts": {
  "ng": "ng",
  "start": "ng serve --port 4300 --host 0.0.0.0 --proxy-config proxy.conf.json",
  "build": "ng build",
  "test": "ng test",
  "lint": "ng lint",
  "e2e": "ng e2e"
}

该应用程序可访问以下网址:

http://localhost:4300/#/index.html

我想在上下文根上提供它,例如:“/myApp/”

应用程序必须始终以 开头npm start,我想访问 url:

http://localhost:4300/myApp/#/index.html

我该怎么做?

我尝试的一切都不起作用:

任何帮助将不胜感激。

标签: angularcontextpathng-buildcontextroot

解决方案


添加baseHref,deployUrl在您的 angular.json 文件中。添加代码片段:

"serve": {
     "options": {
        "baseHref": "/myApp/",
        "deployUrl": "/myApp/"
      }
    }

<base href="/myApp/">将保持原样。

您可以--base-href=myApp从构建命令中删除


推荐阅读