在删除井号之前,我已经
mainApp.config(function ($locationProvider, $routeProvider) {
$routeProvider
.when('/page', {
controller: 'Page',
templateUrl: 'templates/page.html'
})
.when('/main', {
controller: 'Main',
templateUrl: 'templates/main.html'
})
.otherwise({ redirectTo: '/main'});
//$locationProvider.html5Mode(true);
});
这些工作正常
http://localhost:8080/index.html#/main
http://localhost:8080/index.html#/page
删除井号后,我将其添加到index.html
<base href="/">
<script src="/vendor/bootstrap-dist/js/bootstrap.min.js"></script>
<script src="/vendor/javascript/angular/angular.js"></script>
<script src="/vendor/javascript/angular/angular-route.js"></script>
<script src="/vendor/javascript/angular/ui-bootstrap-tpls-0.11.2.min.js"></script>
<script src="/javascript/index.js"></script>
<script src="/javascript/controllers/main.js"></script>
<script src="/javascript/controllers/page.js"></script>
和index.js
$locationProvider.html5Mode(true);
现在点击http://localhost:8080
重定向到http://localhost:8080/main
但http://localhost:8080/main
直接在浏览器中返回404以及其他页面
我应该怎么做才能解决这个问题?
我的后端是Java
那是意料之中的。这是未打开html5时发生的情况:
http://localhost:8080/index.html#/main
在地址栏中输入网址现在,当您启用html5模式并加载index.hml时会发生什么?
http://localhost:8080/index.html
在地址栏中输入网址现在,如果您单击刷新或直接http://localhost:8080/main
在地址栏中输入会发生什么?那么,在这种情况下,您是在说浏览器:“请在url上加载页面http://localhost:8080/main
。这就是浏览器的工作:它将HTTP请求发送到http://localhost:8080/main
。由于服务器在该地址没有任何内容,因此它将发回404。
现在如何使其工作?实际上非常简单:您需要配置服务器,使其在收到路径/main
(以及角度应用程序的所有其他路径)请求时发回index.html页面。这样,浏览器将加载HTML页面,包含的角度应用程序将重新启动,角度路由器将从/main
URL解析路径(),从而将加载与该路径关联的视图和控制器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句