在我的 SPA AngularJS 应用程序中,我得到的 URL 为 http://localhost:4200/#!/
而不仅仅是# (hash bang)
。因此,路由似乎无法以通常的方式工作。
我确实经历过这个 stackoverflow Question但没有找到任何解决方案。有人知道摆脱这个额外感叹号的解决方案吗?
编辑:
在我的index.ejs:
我有<a href="#about">about</a>
在我的approutapp.js:
我有
var myapp = angular.module("myApp", ["ngRoute"]);
myapp.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "/static/list.ejs"
})
.when("/about", {
templateUrl : "/static/about-us.ejs"
})
});
myapp.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}]);
但是我仍然得到的 URL:http://localhost:4200/#/!/
然后前往http://localhost:4200/about
页面挂起
最佳答案
尝试使用$locationProvider.hashPrefix('')
原因是:
如果浏览器是HTML5浏览器angularJS
会将其重定向到#!
否则就只是#
。
阅读 $location 上的此文档了解更多信息:
Opening a regular URL in a legacy browser -> redirects to a hashbang
URL Opening hashbang URL in a modern browser -> rewrites to a regular URL
HTML5 mode
In HTML5 mode, the
$location
service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents. If the HTML5 History API is not supported by a browser, the $location service will fall back to using the hashbang URLs automatically. This frees you from having to worry about whether the browser displaying your app supports the history API or not; the $location service transparently uses the best available option.Opening a regular URL in a legacy browser -> redirects to a hashbang URL Opening hashbang URL in a modern browser -> rewrites to a regular URL Note that in this mode, AngularJS intercepts all links (subject to the "Html link rewriting" rules below) and updates the url in a way that never performs a full page reload.
关于javascript - AngularJS SPA 路由获取 URL 为 "/#!"而不是 "#",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480753/