我想为我的应用启用HTML5模式。我已经为配置放了以下代码,如图所示
here:
- return app.config(['$routeProvider','$locationProvider',function($routeProvider,$locationProvider) {
- $locationProvider.html5Mode(true);
- $locationProvider.hashPrefix = '!';
- $routeProvider.when('/',{
- templateUrl: '/views/index.html',controller: 'indexCtrl'
- });
- $routeProvider.when('/about',{
- templateUrl: '/views/about.html',controller: 'AboutCtrl'
- });
如你所见,我使用$ locationProvider.html5模式,我改变了所有的链接在ng-ref排除/#/。
问题
目前,我可以去localhost:9000 /和看到索引页,并导航到其他页像localhost:9000 / about。
但是,当我刷新本地主机:9000 / about页时,会出现问题。我得到以下输出:不能GET /约
如果我看网络电话:
- Request URL:localhost:9000/about
- Request Method:GET
如果我先去localhost:9000 /然后点击一个按钮导航到/ about我得到:
- Request URL:http://localhost:9000/views/about.html
这将完美地呈现页面。
先谢谢你。
从
angular docs
Server side
Using this mode requires URL rewriting on server side,basically you have to rewrite all your links to entry point of your application (e.g. index.html)
原因是当您第一次访问该网页(/ about)时,刷新后,浏览器无法知道这不是一个真正的URL,所以它继续加载它。然而,如果你已经加载了根页面和所有的JavaScript代码,那么当你导航到/ about Angular可以在浏览器尝试打开服务器并处理它之前