小编flo*_*ppe的帖子

脚本 src 的 HTML 相对路径问题

我对从 Angular 构建的网站有疑问。在 中index.html,有对 javascript 文件的引用,但我的问题是:在 HTML 中,路径相对于文件,但浏览器在根目录中查找文件:

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>RasaBE</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">

  <!-- <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  -->
</head>
<body>
  <app-root></app-root>
<script type="text/javascript" src="inline.bundle.js"></script><script type="text/javascript" src="polyfills.bundle.js"></script><script type="text/javascript" src="styles.bundle.js"></script><script type="text/javascript" src="vendor.bundle.js"></script><script type="text/javascript" src="main.bundle.js"></script></body>
</html>
Run Code Online (Sandbox Code Playgroud)

当所有内容都位于我网站的根文件夹中时,这种方法效果很好,但我想将所有内容都放在子文件夹中/bo/。所有脚本也是index.html如此。localhost/bo/index.html由于它是代码中的相对路径,我希望我的浏览器查找localhost/bo/script.js,但它却查找localhost/script.js.

显示问题所在的屏幕截图:

它不会在正确的目录中查找文件

我在 Chrome 中对此进行了测试,并且遇到了同样的问题。直接更改路径有点复杂,因为它是从角度编译的,而且我可能每次编译时都必须更改它。这是 Firefox 及其处理相对路径方式的问题吗?

我还应该添加它host/bo/inline.bundle.js存在并且可以被浏览器找到,但它在根文件夹而不是与index.html.

html javascript

4
推荐指数
1
解决办法
7110
查看次数

标签 统计

html ×1

javascript ×1