除了--serve-path 选项,您还需要指定--base-href 或--deploy-url 选项,否则您的资产将具有无效的源URI。没有它们,您的 index.html 文件包含类似
<head>
<base href="/">
...
</head>
<body>
...
<script src="vendor.js" defer></script>
<script src="main.js" defer></script>
</body>
如您所见,所有资产都有不正确的 /vendor.js、/main.js 等 URI,而不是正确的一个 /foobar/vendor.js、/foobar/main.js 等。
如果你使用ng serve --serve-path=/foobar/ --base-href=/foobar/,index.html 看起来像
<head>
<base href="/foobar/">
...
</head>
<body>
...
<script src="vendor.js" defer></script>
<script src="main.js" defer></script>
</body>
如果你使用ng serve --serve-path=/foobar/ --deploy-url=/foobar/,同样的index.html 将看起来像
<head>
<base href="/">
...
</head>
<body>
...
<script src="/foobar/vendor.js" defer></script>
<script src="/foobar/main.js" defer></script>
</body>
在现代 Angular 版本中,这两个选项都被视为 deprecated(但仍可使用当前的 Angular CLI 12),建议在 angular.json 配置文件中使用构建器配置选项:
...
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"baseHref": "/foobar/",
...
Angular 文档说使用baseHref 通常比deployUrl 更好。