繁体   English   中英

Angular.js-如何从URL中删除#-错误:无法在“历史记录”上执行“ pushState”:

[英]Angularjs - How to remove the # from the url - Error: Failed to execute 'pushState' on 'History':

我在其他帖子中尝试了有关删除片段标识符(#)的所有内容。 一切都可以使用#标识符进行,但对我的项目而言这将是一个不错的改进。

  1. 我配置了$ locationProvider并将html5Mode设置为true
  2. 在我的index.html中设置<base href="app" /> ,也可以尝试<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使用的节点服务器上启动该应用程序后,它部分工作。 在单击刷新页面后,页面上仅显示一条短消息:“无法获取/...__部分视图名称*”

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

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

也可以看看

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

如果您现在拥有一个处理请求的节点服务器,则需要配置该节点服务器以处理重定向,但是个人而言仅使用Apache和Nginx完成了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM