没有散​​列就无法获得 Backbone 路由? [英] Can't get Backbone routes without hashes?

查看:24
本文介绍了没有散​​列就无法获得 Backbone 路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要浏览器可以捕获和处理的可添加书签的 URL.如果我只使用 Backbone.history.start(),那么我可以使用哈希 URL,例如 /#accounts.

但我想要没有哈希值的 URL,比如 /accounts.但我无法使用 Backbone.history.start( { pushState: true } ) (正如其他人所描述的).我的路线很简单,直接取自文档.

<前>MyRouter = Backbone.Router.extend({路线:{'/accounts': '账户',}});

我正在使用 Chrome(也尝试使用 FF),行为是 /accounts 请求直接发送到服务器.没有先被 Backbone 拦截.有没有人遇到过这个?如何得到使用 Backbone 进行无哈希 URL 处理?

提前致谢

解决方案

您将使用 router.navigate( "/accounts", true ) 使用 js 导航到该 url,而不是通过链接或输入自己的网址.要使用链接,您必须将点击事件绑定到它们并阻止默认操作并调用导航到链接 href.

routerRouter

的一个实例

I want to have bookmarkable URLs that the browser can capture and handle. If I just use Backbone.history.start(), then I can use hash URLs, like /#accounts.

But I want URLs without the hashes, a la /accounts. But I can't get this to work using Backbone.history.start( { pushState: true } ) (as others have described it). My routes are straightforward, and taken directly from the documentation.


    MyRouter = Backbone.Router.extend({
        routes: {
            '/accounts': 'accounts',
        }
    });

I'm using Chrome (also tried with FF), and the behaviour is that an /accounts request goes straight to the server. Not being intercepted by Backbone first. Has anyone run into this? How do I get hash-less URL handling with Backbone?

Thanks in advance

解决方案

You would navigate to that url with js using router.navigate( "/accounts", true ), not by links or entering the url yourself. To use links, you must bind a click event to them and prevent the default action and call navigate to the links href.

router is an instance of Router

这篇关于没有散​​列就无法获得 Backbone 路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆