HTML 头部的 base-href 似乎被加载的所有资产忽略

base-href in head of HTML seems to be ignored for all assets loaded

也许我的整个想法都错了,所以让我们检查一下。我有以下 HTML

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <base href="/xyz"> 
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
    <app-root></app-root>
    <script src="runtime.js" defer=""></script><script src="polyfills.js" defer=""></script><script src="main.js" defer=""></script></body>
</html>

现在,当我尝试使用以下 url 在浏览器中加载它时:http://localhost:8080/xyz 它加载 index.html 但对于所有资产(样式和 javascript 文件)我得到 404

我希望,因为我有 base href="/xyz" 它会在所有资产 url 前加上 url,但如您所见,它没有。

为了完整起见,这是我的文件结构:

./server.js
./xyz/index.html
      styles.css
      .....

所以服务器正在提供来自 /xyz 路径

的所有文件

相对路径通过采用基数 URL (http://localhost:8080/xyz) 来解析,删除路径中最后一个 / 之后的所有内容(给你 http://localhost:8080/),然后附加新路径 (http://localhost:8080/styles.css).

如果你想要/xyz/styles.css那么你需要在你的基本路径的末尾放一个/<base href="/xyz/">