javascript - 如何在 AngularJS 中指定基本 url 中的相对路径

标签 javascript html css angularjs

我正面临 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>

Angular Docs $location/nobase

Angular Docs $location

关于绝对路径,<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/

相关文章:

html - 自动宽度并排的 DIV 定位问题。

javascript - 如何识别何时在弹出窗口外进行了点击?

jquery - 固定 div 在滚动时在未固定的 div 下消失

css - Bootstrap 复杂列位置排序

javascript - 在 Meteor Js 中,如何将查询字符串中的推荐代码添加到用户个人资料中?

javascript - 如何在 Elementor 中添加灯箱弹出窗口

javascript - 在播放 youtube 视频时淡出页面

html - 向具有背景图像的 HTML anchor 添加填充

css - 当与它对齐的 div 的宽度可能不同时,如何防止 div 换行?

JavaScript通过循环更新iframe src以下载多个文件