我正面临 AngularJS 路由的非常奇怪问题。我有一定的文件夹结构
js -> Angular ->
css -> Bootstrap ->
index.html
当托管在某些服务器(如 IIS)上时,所有路由都运行良好。但我复制了整个指令并粘贴到其他位置或与他人分享。如果他们使用文件系统运行,他们会开始收到文件未找到错误。
这是我的 index.html 的样子。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<base href="" />
<!--css-->
<link href="./css/Bootstrap/bootstrap.min.css" rel="stylesheet" />
</head>
<body ng-app="myApp">
<div class="container-fluid">
<!--content-->
<!--menu tabs-->
<div class="col-md-12" style="margin-top:10px;">
<a href="/car"> CAR</a> <br/>
<a href="/mobile">Mobile</a>
</div>
<!--angular template placeholder-->
<div ng-view> </div>
<!--footer-->
</div>
<!--js-->
<script src="./js/Angular/Lib/angular.js"></script>
<script src="./js/Angular/Lib/angular-route.js"></script>
<script src="./js/Angular/Config/ngRouteConfig.js"></script>
只有当我在 html 中有 base 标签时才会出现此 404 问题。如果我删除它,页面呈现罚款。但这会停止运行 Angular 路由。
我收到一封来自上级的电子邮件,说我不应该指定绝对路径,但我在我的 index.html 中找不到我指定绝对路径的任何地方。
那么如何指定相对路径,以便具有相同文件夹结构的任何人都可以运行演示应用程序?
高度赞赏任何帮助/建议。
最佳答案
你试过吗:
<base href="/" />
If you are deploying your app into the root context (e.g. https://myapp.com/), set the base URL to /:
<head>
<base href="/">
...
</head>
If you are deploying your app into a sub-context (e.g. https://myapp.com/subapp/), set the base URL to the URL of the subcontext
<head>
<base href="/subapp/">
...
</head>
关于绝对路径,<a href="/car"> CAR</a>
不是绝对路径,但它确实强制浏览器回头查看 Web 服务器的 ROOT 目录以查找路径。因此,如果您与之共享的人将应用程序安装到子目录,那么这很可能会导致错误。
在那种情况下,他们应该设置 <base href="to-wherever-they-put-the-thing" />
在他们的网络服务器上,或者您应该指定在网络服务器的 ROOT 目录中运行它。
关于javascript - 如何在 AngularJS 中指定基本 url 中的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44460636/