当我打开html5Mode时,刷新不工作

When I turn on html5Mode refresh doesn't work

本文关键字:刷新 工作 html5Mode      更新时间:2023-09-26

我有以下问题。我使用的是AngularJS和angular-route。当我打开html5Mode

$locationProvider.html5Mode(true);

为漂亮的URL,如果我刷新页面,我有" page Not Found"错误。

这是我的HTML代码:
<body ng-app="app">
<div class="container" ng-controller="MainCtrl">
    <div class="page-header">
        <h1>Page title <small>© 2014</small></h1>
    </div>
    <a href="/">HOME</a>
    <a href="/contact">CONTACT</a>
    <div ng-view></div>
</div>
</body>

和JS代码:

(function() {
angular.module("app", ["ngRoute"])
    .config(function($routeProvider,$locationProvider){
        $routeProvider
            .when("/",{
                template: "Home!"
            })
            .when("/contact",{
                template: "Contact!"
            });
        $locationProvider.html5Mode(true);
    })
    .controller("MainCtrl", function() {
    });
})();

有什么解决办法吗?

可能,您必须设置重写规则。
您的服务器上没有/contacts,它应该映射到处理程序。

例如,对于Apache,它看起来像(.htaccess文件):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html