navigate不保存参数

navigate不保存参数

本文介绍了Backbone.history.navigate不保存参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一些路线:

routes: {
    "": "main",
    "!/": "main",
    "!/page": "page",
    "!/page/:id": "page"
},

然后我有一些html链接,例如:

Then I have some html link, for example:

<a href="site.com/#!/page/3">my link</a>

如果我按一下我的链接,则可以访问URL site.com/#!/page/3,对我来说也可以.但是,如果我有此链接:

If i press to my link I access to url site.com/#!/page/3 and it's ok for me. But if I have this link:

<a href="javascript:void(0);" onclick="Backbone.history.navigate('!/page/3', {trigger:true});">my link</a>

我可以访问URL site.com/#!/page/(没有id=3). ID仍处于定义状态.但是在重新加载页面后,我的ID出现了问题,因为我有url:site.com/#!/page,并且ID未定义.

I access to url site.com/#!/page/ (without id=3). Id is still defined. But after page reloading, I have a problem with my ID, because I have url: site.com/#!/page, and ID is not defined.

如何使用以下链接标记在Backbone中导航?

How can I navigate in Backbone with the following link tag?

<a href="javascript:void(0);" onclick="Backbone.history.navigate('!/page/3', {trigger:true});">my link</a>

推荐答案

骨干网路由使用片段,就像其他任何锚链接一样.您只需要使用href,因为它应该被使用:

Backbone routing uses fragment just like any other anchor links would. You just need to use the href as it's suppose to be used:

<a href="#!/page/3">my link</a>

当单击这样的链接时,无需将trigger设置为true,因为它是默认行为.骨干监听hashchangepopstate事件,并在必要时触发路由.

When clicking a link like this one, there's no need to set trigger to true as it's the default behavior. Backbone listens for hashchange or popstate events and triggers the route when necessary.

此外,routes哈希可以简化:

routes: {
    "*anything": "main",
    "!/page(/:id)": "page"
},

请参见 routes文档.

这篇关于Backbone.history.navigate不保存参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:43