问题描述
我有一些路线:
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,因为它是默认行为.骨干监听hashchange
或popstate
事件,并在必要时触发路由.
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不保存参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!