本文介绍了了解如何才能去除角路线的哈希#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除井号之前,我对

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

After removing the pound sign, I added to 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

and to index.js

$locationProvider.html5Mode(true);

现在按的http://本地主机:8080 重定向到的http://本地主机:8080 /主

但要的http://本地主机:8080 /主直接在浏览器返回404和其他页面太

but going to http://localhost:8080/main directly in the browser returns 404 and the other pages too

我应该怎么做来解决这个问题?

What should I do to fix the problem?

我的后端是用java

my backend is java

推荐答案

这的预期。下面是HTML5的时候没有打开会发生什么:

That's expected. Here's what happens when html5 is not turned on:


  • 您输入网址的http://本地主机:在地址栏8080 / index.html的#/主

  • 浏览器发出HTTP请求到localhost:8080 / index.html,然后得到html页面作为响应

  • HTML网页包含被执行的角度应用。角路由器哈希(/主)后,解析路径,从而加载相关的视图和控制器

当您启用HTML5模式,并加载index.hml现在发生了什么?

Now what happens when you enable html5 mode and load index.hml?


  • 您输入网址的http://本地主机:8080 / index.html的在地址栏中

  • 浏览器发出HTTP请求到localhost:8080 / index.html,然后得到html页面作为响应

  • HTML网页包含被执行的角度应用。角路由器解析路径(/index.html),发现它不匹配任何路线,因而在地址栏默认改变位置:/主,然后加载相关的视图和控制器。在地址栏更改位置不会使浏览器做的比加在其导航历史上的一个新条目的任何其他。

现在,如果你打刷新或直接进入,会发生什么的http://本地主机:8080 /主在地址栏?那么在这种情况下,你说的浏览器:请在网址加载网页的http://本地主机:8080 /主所以这是浏览器做了什么:它发送一个HTTP请求到的http://本地主机:8080 /主由于服务器没有这个地址什么,它发回了404

Now, what happens if you hit refresh, or directly enter http://localhost:8080/main in the address bar? Well in that case, you're saying the browser: "please load the page at the url http://localhost:8080/main. So that's what the browser does: it sends an HTTP request to http://localhost:8080/main. Since the server doesn't have anything at this address, it sends back a 404.

现在如何工作?这其实很简单:你需要配置服务器来发送回index.html页面时,得到的路径请求 /主(和所有其他路径角应用程序)。这样一来,浏览器将加载HTML页面,它包含的角度应用程序将重新启动,角路由器将解析路径( /主)从URL,它从而将加载视图和相关的这条道路控制器。

Now how to make it work? It's actually quite simple: you need to configure the server to send back the index.html page when it gets a request for the path /main (and for all the other paths of the angular application). That way, the browser will load the HTML page, the angular application it contains will be restarted, the angular router will parse the path (/main) from the URL, and it will thus load the view and the controller associated to that path.

这篇关于了解如何才能去除角路线的哈希#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:51