【问题标题】:hashbang urls not workinghashbang url 不起作用
【发布时间】:2016-08-23 09:04:26
【问题描述】:

我使用 angular ui 路由,工作正常 - 但在不支持 html5mode 的浏览器中,它必须退回到 hashbang,链接不起作用。

www.test.com/something

www.test.com/#/something

不太确定如何使 hashbanged 链接起作用?

我绝望的尝试:

if (window.history && window.history.pushState) {
  // HTML5 history API is available.
  $locationProvider.html5Mode({
    enabled: true,
});
} else {
    // hashbang mode.
    window.location.hash = '/'; 
    $locationProvider.html5Mode({
      enabled: true,
    });
  }

$urlRouterProvider.otherwise("/");

$stateProvider
  .state('statistics', {
      url: "/statistics/:id",
      templateUrl: '../path/statistics.html',
      controller: 'ResultCtrl'
    }
);

【问题讨论】:

  • @CallumLinington 读了github.com/angular-ui/ui-router 文档,而docs.angularjs.org/guide/$location 没有运气;我
  • 但是,如果您阅读该标题下的$location service configuration,它会明确告诉您如何启用 hashbang 模式...
  • 是不是templateurl中的相对路径有问题?你的 html 中有基本标签吗?
  • 是的,我脑子里有一个基本标签。指向 index.html
  • 不应该指向“/”吗?

标签: angularjs hashbang


【解决方案1】:

你只需要这样做:

$locationProvider.html5Mode({
    enabled: true,
}).hashPrefix("#");

【讨论】:

  • 通常你会有一个“!”在 hashprefix 中获取经典的 #!哈希棒。拥有 .hashprefix('#') 只是在 url 中添加了第二个标签 -> www.something.com/##/view1
  • 是的@AndersPedersen 但他要求 #/something 所以我相信他希望它与 # 一起使用
【解决方案2】:

您已启用 html5mode。这种模式基本上意味着该站点不使用 hashbang url 进行路由。我认为你不能同时支持两者。

删除此代码:

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

更新

https://docs.angularjs.org/guide/$location#html5-mode

根据您不需要测试推送状态支持的文档,如果不支持,它会自动回退到 hashbangs。

【讨论】:

  • 如果 html5mode 不可用(使用旧版浏览器),它将自动从我可以从文档中读取的内容返回到 hashbang 模式。
  • 你用我刚刚告诉你的内容更新了帖子? :) 检查的原因是如果不支持 html5mode,则拼命尝试在没有 # 的情况下编辑 URL。
  • 是的,当您发表评论时,我正在更新它。如果在您的代码中某处调用 $location.path('/somethingvalid') (在 ie9 中)它会尝试浏览到什么路径?
猜你喜欢
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 2016-04-08
  • 2012-08-10
  • 1970-01-01
相关资源
最近更新 更多