我有一个类似 mysite.com/en/bla-bla/bla/page#someid 的网址 如果在哈希 URL 中找到 someid,浏览器应该将窗口滚动到某个页面位置。
此外,我的页面上有主干,如果用户打开 URL 主干中包含某些哈希值的页面,则无法如果没有定义的路由与当前 URL 匹配。 Backbone 无法将 #someid 作为路径进行匹配。
var result = Backbone.history.start();
if (!result) {
console.log("If no defined route matches the current URL");
}
如何解决?
最佳答案
您可以添加一个包罗万象的 route :
splat parts
*splat
, which can match any number of URL components.
var Router = Backbone.Router.extend({
routes: {
'*catchall': 'homeRoute',
// any route defined further down takes precedence on the ones before.
'real-route/:id': 'realRoute',
},
homeRoute: function() { /*...*/ },
realRoute: function(id) { /*...*/ }
});
定义您希望 Backbone 处理的任何路由,然后让 splat 捕获任何未定义的路由。
关于javascript - 如何忽略 Backbone.history.start 上 URL 中的哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40789974/