【问题标题】:Handling routes without hashbang using angular-ui-router使用 angular-ui-router 处理没有 hashbang 的路由
【发布时间】:2015-06-28 00:11:13
【问题描述】:

我在网上搜索了很多答案,但还没有找到任何答案。

现在我的文件中有这段代码,我认为它可以处理除我指定的路径/状态之外的所有路由/状态:

$urlRouterProvider.otherwise('/');

但是,此功能仅适用于 localhost:1337/#/stuff 之类的路径,但如果我输入 localhost:1337/stuff,我会收到一个丑陋的内部服务器错误。

理想情况下,我希望 localhost:1337/stuff 或任何其他没有 hashbang 的 URL 重定向到 localhost:1337/#/(我的应用主页)。

有什么想法可以做到这一点吗?

【问题讨论】:

    标签: javascript angularjs angular-ui-router


    【解决方案1】:

    你需要设置两件事:

    1. AngularJS 配置中的所谓“html5Mode”。这是最简单的部分:$locationProvider.html5Mode(true);
    2. 在您的服务器上设置重写,以便任何请求仅呈现您的主 html 页面。

    来自 AngularJS docs on routing:

    使用此模式需要在服务器端重写 URL,基本上您必须重写所有指向应用程序入口点的链接(例如 index.html)。

    【讨论】:

    • 感谢您的回答!我想这可能超出了我的项目范围。
    【解决方案2】:

    您可以在 app.js 中启用 html5 模式,这也会从项目中的链接中删除 hashbang(localhost:1337/stuff 而不是 localhost:1337/#/stuff)。

    如果这不是您想要的行为,那么您将不得不在服务器端进行重写。

    【讨论】:

      猜你喜欢
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 2013-11-26
      • 2014-03-04
      • 2015-02-26
      • 2017-09-15
      • 1970-01-01
      相关资源
      最近更新 更多