gpt4 book ai didi

backbone.js - pushState 有什么用?

转载 作者:行者123 更新时间:2023-12-03 15:50:43 26 4
gpt4 key购买 nike

我看到最新的backbone.js(0.5)引入了路由的pushState选项。

通读后https://developer.mozilla.org/en/dom/manipulating_the_browser_history
我不得不说我不是很清楚:在编写带有主干的 Web 应用程序的上下文中,pushState 是什么以及 pushState 带来了什么;是为了:

  • 改进网址:拥有一个“真实的”、可收藏的、“服务器可访问的”网址,而不是散列?
  • 优雅降级:允许服务器在不启用 JS 的情况下呈现正确的页面?
  • 以上两者/都不是,或其他原因?

  • 另外,我在下面做错了什么?:
    class MyRouter extends Backbone.Router
    routes :
    '' : 'index'
    '#hello' :'hello'

    index : -> console.log 'index'
    hello: -> console.log 'hello'

    new MyRouter

    Backbone.history.start pushState: true

    当我导航到 http://localhost#hello , url 改为 http://localhost/#hello ,但没有触发回调?

    谢谢

    最佳答案

    您不需要路由表中的 # 前缀。尝试这个:

      routes :
    '' : 'index'
    'hello' : 'hello'

    至于 pushState,我认为它是以上两者。这确实意味着服务器端的工作比处理位置哈希的工作要多,因为您将必须确保您的服务器可以为所有这些 URL 提供页面。

    关于backbone.js - pushState 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6556463/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com