0

启用后$locationProvider.html5Mode(true);<base href="/" />index.html路由中手动重新加载后开始崩溃。

寻找解决方案我发现.htaccess规则可以解决这个问题,但不同的配置没有帮助。

也许这是因为我index.html在子目录中/views/index.html

.htaccess

Options +FollowSymLinks

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteRule (.*) index.html [L] # /views/index.html doesn't work as well
</ifModule>

route config

app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);

    $routeProvider
        .when('/', {
            templateUrl: 'home.html',
            controller: 'AddTaskCtrl',
            controllerAs: 'AddTask'
        })
        .when('/chat', { //this route breaks on manual refresh  
            templateUrl: 'chat.html',
            controller: 'ChatCtrl',
            controllerAs: 'chat'
        })
}]);
4

1 回答 1

1

第 1 步:仅使用 $locationProvider.html5Mode(true);

第 2 步:更改您的 .htaccess

RewriteEngine on

# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]

当 index.html 开始加载您的应用程序时,或查看更多方法https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to -work-with-html5mode

于 2016-05-14T14:40:06.980 回答