0

我尝试了其他帖子中有关删除片段标识符 (#) 的所有内容。使用 # 标识符一切正常,但这对我的项目来说将是一个很好的改进。

  1. 我配置了 $locationProvider 并将 html5Mode 设置为 true
  2. <base href="app" />在我的 index.html 中设置也试过了<base href="/" />。我用了<base href="/" />
  3. 不设基地 requireBase: false
  4. $locationProvider.html5Mode(true)正如有人建议的那样,检查浏览器if(window.history && window.history.pushState)对 html5 历史 API 的支持
  5. 我按照其他人的建议在 web.config 中编写了一条规则。
  6. 我用谷歌搜索后尝试了所有我能找到的东西。

我收到此错误:

angular.js:13708 错误:无法在“历史”上执行“pushState”:无法在源为“ http://localhost ”的文档中创建URL 为“ http://sales/?_ijt=d1r1ladp5ro1tvflefsdpnfvd4 ”的历史状态对象:52471 ' 和 URL ' http://localhost:52471/BeerbayCRM/BeerbayCRM/app/index.html?_ijt=d1r1ladp5ro1tvflefsdpnfvd4 '。

请参阅可用环境变量列表非常感谢您的帮助!谢谢你。

这是我的代码:

app.config(['$routeProvider', '$locationProvider', '$httpProvider',
  function($routeProvider, $locationProvider) {

    $routeProvider
      .when('/', {
        templateUrl: "views/productsView.html"
      })
      .when("/products", {
        templateUrl: "views/productsView.html"
      })
      .when("/sales", {
        templateUrl: "views/salesView.html"
      })
      .when("/charts", {
        templateUrl: "views/chartsView.html"
      })
      .otherwise({
        templateUrl: "views/productsView.html"

      });

    if (window.history && window.history.pushState) {

      $locationProvider.html5Mode({
        enabled: true
      });
    }
  }
])

.controller("routeCtrl", function($scope, $location) {
  $scope.setRoute = function(route) {
    $location.path(route);
  }
});
<!DOCTYPE html>
<html ng-app="myApp">

<head>

  <!--.....all the dependencies are here.....-->

  <base href="app" />

</head>

<body ng-controller="myAppCtrl">
  <div class="navbar navbar-inverse">
    <a class="navbar-brand" href="#">Beerbay</a>
  </div>
  <div ng-controller="routeCtrl" class="col-sm-1">
    <a ng-click="setRoute('products')" class="btn btn-block btn-primary btn-lg">Products</a>
    <a ng-click="setRoute('sales')" class="btn btn-block btn-primary btn-lg">Sales</a>
    <a ng-click="setRoute('charts')" class="btn btn-block btn-primary btn-lg">Charts</a>
  </div>
  <ng-view/>
</body>

</html>

更新:在我在与 WebStorm 使用不同的节点服务器中启动应用程序后,它部分工作。在我点击刷新页面后,我在页面上只收到一条短消息:“Cannot GET /...__partial view name*”

4

1 回答 1

1

您需要在适当的位置进行服务器端重定向......默认情况下,当服务器收到对 example.com/yoursite/some-page 的请求时,它只会尝试在找不到时从该位置加载一些索引文件它们将是 404。您需要在 Web 服务器配置中设置服务器端重定向,以便任何以 /yoursite/ 开头的丢失路径重定向到 /yoursite/index.html 以便您的角度路由可以处理它。

https://docs.angularjs.org/guide/$location#server-side

也可以看看

使用 AngularJS HTML5 模式重新加载页面会给出错误的 GET 请求

如果你现在有一个处理请求的节点服务器,你需要配置节点服务器来处理重定向,不过个人只用 Apache 和 Nginx 完成了。

于 2016-08-18T19:15:17.370 回答