AngularJS SPA 路由得到 URL 作为“/#!”代替 ”#”
AngularJS SPA routing getting URL as "/#!" instead of "#"
在我的 SPA AngularJS 应用程序中,我将 URL 作为 http://localhost:4200/#!/
而不仅仅是 # (hash bang)
。因此,路由似乎无法正常工作。
我确实浏览了这个 Whosebug Question 但没有找到任何 solution.Anybody 知道摆脱这个额外感叹号的解决方案吗?
编辑:
在我的 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.
此更改可以在 angular 版本 > 1.6.x 中观察到。在以前的版本中,它就像 #
(散列爆炸)而已。
要获得 url 就像 #
的方式,你可以简单地这样写,
appModule.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}]);
此更改是在 version 1.6.0
中引入的,您可以看到 https://github.com/angular/angular.js/blob/master/CHANGELOG.md#location-due-to。
此外,在这里添加那里写的内容:
The hash-prefix for $location
hash-bang URLs has changed from the empty string "" to the bang "!". If your application does not use HTML5 mode
or is being run on browsers that do not support HTML5 mode
, and you have not specified your own hash-prefix then client side URLs will now contain a "!" prefix. For example, rather than mydomain.com/#/a/b/c will become mydomain.com/#!/a/b/c.
在我的 SPA AngularJS 应用程序中,我将 URL 作为 http://localhost:4200/#!/
而不仅仅是 # (hash bang)
。因此,路由似乎无法正常工作。
我确实浏览了这个 Whosebug Question 但没有找到任何 solution.Anybody 知道摆脱这个额外感叹号的解决方案吗?
编辑:
在我的 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.
此更改可以在 angular 版本 > 1.6.x 中观察到。在以前的版本中,它就像 #
(散列爆炸)而已。
要获得 url 就像 #
的方式,你可以简单地这样写,
appModule.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}]);
此更改是在 version 1.6.0
中引入的,您可以看到 https://github.com/angular/angular.js/blob/master/CHANGELOG.md#location-due-to。
此外,在这里添加那里写的内容:
The hash-prefix for
$location
hash-bang URLs has changed from the empty string "" to the bang "!". If your application does not useHTML5 mode
or is being run on browsers that do not supportHTML5 mode
, and you have not specified your own hash-prefix then client side URLs will now contain a "!" prefix. For example, rather than mydomain.com/#/a/b/c will become mydomain.com/#!/a/b/c.