gpt4 book ai didi

javascript - 主干 router.navigate 不重定向

转载 作者:行者123 更新时间:2023-11-30 10:06:10 25 4
gpt4 key购买 nike

老实说,我觉得问这个问题有点傻,因为应该是一件非常简单的事情......在我的应用程序初始化中,我有:

window.myApp =
Models: {}
Collections: {}
Views: {}
Routers: {}
initialize: ->
window.router = new Backbone.Router

Backbone.history.start(pushState: true)

然后在我的 View 中,当我单击一个按钮时,我会:

router.navigate('/profile')

我也试过

router.navigate('/profile', true)

现在...我知道这不是使用 Backbone 路线的最佳方式,但我需要我的 Rails 应用程序管理路线并且我仅使用 Backbone 路线来获得路线历史...如果我执行 window.href = '/profile' 它会正确重定向。

我在主干路由上做错了什么?我以前用过这种方法,但我不明白为什么现在它不再起作用了。

编辑:新网址始终正确显示在导航栏中,但它不会重定向到新页面...仅在导航栏中显示新网址。

最佳答案

Backbone router.navigate doesn't redirect

不,它没有。这不是router.navigate 甚至 的事情。它与重定向无关,它用于更新地址栏。

The new url is always correctly show in the navigation bar, but it doesn't redirect to the new page...only show the new url in the navigation bar.

是的,这正是router.navigate 被记录要做的事情。这是它的明确目的。您可以使用它来更新 URL 以反射(reflect)页面的当前状态,而不是将页面重定向到其他内容。

如果你想重定向到一个新页面,你应该使用 window.location.href = '/profile'

如果你想更新地址栏然后触发新一轮的 Backbone 路由,使用 router.navigate('/profile', { trigger: true });,但还是那句话不是 Backbone 的工作方式。

关于javascript - 主干 router.navigate 不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29055763/

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