删除 hashbangs 后直接导航到 url 不起作用 [英] Navigating to url directly after removing hashbangs not working

查看:26
本文介绍了删除 hashbangs 后直接导航到 url 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序使用 angular

I am using angular for my app

我想从 url 中删除 #,所以我按照 SO answer

I wanted to remove the # from the url so i added the below lines as suggested in SO answer

$locationProvider.html5Mode({
  enabled: true,
  requireBase: false
});

在 index.html 中,我还按照此处

In the index.html I also added the below code as suggested here

<head>
  <base href="/">
</head>

当我从主页导航到页面时一切正常,但是当我复制 url 并在新选项卡中打开它时,它会抛出 404 错误

It all works fine when I navigate to pages from the home, but when i copy the url and open it in new tab, it throws 404 error

示例

当我启动应用程序时,它会打开 http://localhost:portno/home.当我刷新页面时,我收到了 404 错误.

When I launch the app, it's opening http://localhost:portno/home. When I refresh the page, I'm getting a 404 error.

我还应该做哪些其他配置?

What other configuration should i make?

我的代码结构如下

  .state('tab.home', {
            url: '/home',
            views: {
                'tab-home': {
                    templateUrl: 'templates/tab-home.html',
                    controller: 'templeHome'
                }
            }
        })
        .state('tab.list', {
            url: '/list',
            views: {
                'tab-home': {
                    templateUrl: 'templates/list.html',
                    controller: 'templeList'
                }
            }
        })

推荐答案

您需要在您的服务器上添加一个 route 将您重定向到前端的入口点(即:index.html).

You need to add a route on your server that will redirect you to the entrypoint of your front (i.e: index.html).

例如,如果你从你家被重定向到 http://localhost:portno/foo/bar,你需要一个 route 来匹配 http://localhost:portno/foo/barcode>/foo/bar 一个将您重定向到您的 index.html.

For example, if you were redirected from your home to http://localhost:portno/foo/bar, you'll need a route to match the /foo/bar one that will redirect you to your index.html.

它可能看起来像这样(请注意,这是我自己为 Hapi 编写的示例代码):

It migth look like this (note that this is an example code of my own written for Hapi):

server.route([
    {
        method: 'GET',
        path: '/foo/bar',
        handler: function(request, reply) {
            reply.file('./public/index.html');
        }
    }
    ...

这篇关于删除 hashbangs 后直接导航到 url 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆