我对angularJs中的路由器有一些疑问。我用了angular-ui-router。但是使用$ locationProvider删除网址中的hashtag(#)时出现错误。如何解决此问题?,在我的模块中:
.config(function($stateProvider, $urlRouterProvider, $locationProvider){
$stateProvider
.state('locale',{
url: '/locale',
templateUrl: 'template/static/locale.html'
})
.state('callback',{
url: '/callback',
templateUrl: 'template/static/callback.html',
controller: 'userCalendar'
})
.state('sign_in',{
url: '/sign_in',
templateUrl: 'template/users/sign_in.html'
})
.state('addprofile', {
url: '/addprofile',
templateUrl: 'template/users/addprofile.html'
})
.state('tabs',{
url: '/tabs',
templateUrl: 'template/pages/tabs.html'
})
.state('tabs.mainMenu', {
url: '/mainMenu',
templateUrl: 'template/pages/mainmenu.html'
})
.state('form',{
url: '#/form',
templateUrl: '/template/users/form.html'
})
$urlRouterProvider.otherwise('locale');
$locationProvider.html5Mode({
enabled: true,
requireBase: true
});
});
感谢您的关注
最佳答案
删除哈希路由后,您将遇到此刷新问题(即使没有Cordova)。我看到您已经在$ app.js
配置中安装了具有requireBase的$ locationProvider设置:
$locationProvider.html5Mode({enabled: true, requireBase: true});
这意味着您还需要在
index.html
中设置基本URL:<html ng-app="myApp">
<head>
<title>AngularJS</title>
<base href="http://localhost:8888/angularjs-website-folder/">
您还需要配置服务器URL重写规则。这是在网站文件夹中使用
.htaccess
文件的MAMP本地主机的解决方案:<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /angularjs-website-folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(css|js|html|png|jpg|jpeg|gif|txt)
RewriteRule (.*) index.html [L]
</IfModule>
注意:第一个RewriteBase应该是一个相对链接,而index.html中的基本url是绝对的。