哈希邦网址不起作用



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

www.test.com/something <作品>

www.test.com/#/something

不太确定如何使哈希链接工作?

我绝望的尝试:

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

你只需要这样做:

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

您已启用 html5mode。此模式基本上意味着该站点不会使用 hashbang 网址进行路由。我不认为你可以同时支持两者。

删除此代码:

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

更新

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

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

最新更新