【问题标题】:Navigating to url directly after removing hashbangs not working删除 hashbang 后直接导航到 url 不起作用
【发布时间】:2015-08-18 19:42:58
【问题描述】:

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

我想从 url 中删除 # 所以我按照SO answer 中的建议添加了以下行

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

在 index.html 中,我还按照建议添加了以下代码 here

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

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

示例

当我启动应用程序时,它正在打开http://localhost:portno/home。 刷新页面时,出现 404 错误。

我应该进行哪些其他配置?

我的代码结构如下

  .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'
                }
            }
        })

【问题讨论】:

  • 我必须试试这个。但我无法得到明确的解决方案。如何克服这个问题

标签: angularjs ionic-framework angular-ui-router


【解决方案1】:

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

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

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

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

【讨论】:

  • 我应该在 app.js 中添加这个吗?
  • 你应该把它添加到你的服务器
  • 基本上我应该将所有直接页面请求重定向到网站的主页,以便从那里保持状态?
  • 是的,所有 Angular 页面都必须重定向到 index.html。 Angular 应用会处理路由本身并显示正确的页面 :)
  • 假设 ./public/index.html 是您的 Angular 应用程序入口点,它应该是的
猜你喜欢
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 2019-02-11
相关资源
最近更新 更多