I want to enable HTML5 mode for my app. I have put the following code for the configuration, as shown here:

return app.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) {

    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix = '!';

    $routeProvider.when('/', {
        templateUrl: '/views/index.html',
        controller: 'indexCtrl'
    });
    $routeProvider.when('/about',{
        templateUrl: '/views/about.html',
        controller: 'AboutCtrl'
    });

正如你所看到的,我使用了$locationProvider.html5mode,我更改了ng-href的所有链接,以排除/#/.

问题

目前,我可以转到localhost:9000/,看到索引页,然后导航到像localhost:9000/about这样的其他页面.

然而,当我刷新localhost:9000/about页时,问题就出现了.我得到以下输出:Cannot GET /about

如果我查看网络呼叫:

Request URL:localhost:9000/about
Request Method:GET

而如果我先go localhost:9000/,然后点击一个按钮导航到/about,我会得到:

Request URL:http://localhost:9000/views/about.html

这使得页面呈现得非常完美.

How can I enable angular to get the correct page when I refresh?

推荐答案

From the angular docs

Server side

原因是,当你第一次访问页面(/about)时,例如刷新后,浏览器无法知道这不是一个真正的URL,因此它会继续并加载它.但是,如果您先加载了根页面和所有javascript代码,那么当您导航到/about时,您可以在浏览器try 访问服务器并相应地处理它之前进入其中

Html相关问答推荐

如何将背景图像放置在窗口的最顶部?

缩小与浮动元素共享空间的HTML元素的边界

如何找到FontAwese图标的Unicode值?

使用网格时图像溢出容器高度

CURL邮箱中的图像不能调整其大小

在网页上的 Select 器中显示选项时出现问题:未在GO模板中传递循环{{range}}的数据

这<;td&>如何溢出<;表>;?

如何翻转卡片图像的背面

响应网格,响应调整大小以适应父div,保留父div S自己的响应高度

如何在R中渲染Quarto文档时动态设置html文件名

Flex:第一个 div 有列,下一个 div 有行

带有图像的虚线边框

当多个a元素用作目标时的:target伪类

文本不显示在 div 下方

子元素放在文本样式之后,样式会 destruct

显示填充正方形的图形的简单页面的 HTML 代码

如何在 css 和 html 中创建花状 struct

pandas `to_html()` - 如何只使特定的行有边框

th 内的 div 标签没有占据全高

如何在 svelte 中对静态 html 文件使用href=