【问题标题】:How do I get a traditional url structure in a single page application (NO hash-bang)?如何在单页应用程序中获得传统的 url 结构(无 hash-bang)?
【发布时间】:2014-05-17 09:27:20
【问题描述】:

我想用 angular.js 构建一个单页应用程序,唯一的事情是我想要一个传统的 url 结构。

例子:

  Before: www.example.com/#/index  
  After:  www.example.com/index  

  Before: www.example.com/#/user/21345678  
  After:  www.example.com/user/21345678  

首先有可能吗?
其次,我该怎么做?
除 Apache XD 以外的任何服务器

【问题讨论】:

    标签: angularjs url routing single-page-application


    【解决方案1】:

    您需要在 Angular 中启用 HTML5 模式。

    .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $locationProvider.html5Mode(true).hashPrefix('!');
    

    那么你还需要配置你的服务器。见这篇文章:Configure your server to work with html5Mode

    我建议阅读这篇短文:Pretty URLs in Angular, Removing the Hashtag。他们还建议在 HTML 的头部设置<base href="/">

    【讨论】:

    • 所以一点 Google foo 发现它只适用于 webkit 浏览器(它可以优雅地回退)但我想知道是否有可能在非 webkit 上实现同样的事情浏览器?
    • 在我当前的项目中,我们没有使用 html5 模式。但是当我们在的时候,我不记得让它在 FF 中工作是一个问题。我更喜欢使用 Chrome,但我在 FF 中进行测试。我觉得这对我来说会很突出。嗯
    猜你喜欢
    • 2018-05-13
    • 2018-12-10
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2023-04-11
    • 2012-04-14
    • 2012-02-13
    • 2012-02-03
    相关资源
    最近更新 更多