本文介绍了Backbone.history.navigate和this.router.navigate之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Backbone.history.navigate this.router.navigate 之间有什么区别?

为什么有时前者会在以后工作?

Why would sometimes the former work while later won't?

推荐答案

如果你看看,你会看到那个 Router.prototype.navigate 只是 Backbone.history.navigate 的代理,你可以在任何地方调用它而不需要对于路由器实例。

If you look at the Backbone source, you'll see that Router.prototype.navigate is simply a proxy to Backbone.history.navigate which you can call anywhere without the need for a router instance.

// Simple proxy to `Backbone.history` to save a fragment into the history.
navigate: function(fragment, options) {
  Backbone.history.navigate(fragment, options);
  return this;
},


路由在 global ,在Backbone中命名空间,历史记录实例。

Routing is handled in a global, namespaced in Backbone, History instance.

这是为了让开发人员创建自己的历史记录 class然后覆盖 Backbone.history 属性以全局更改路由行为。

This is meant to let developer create their own History class and then to overwrite the Backbone.history property to globally change the routing behaviour.

历史类的记录不多,但它是。

The History class isn't documented much, but it's well commented in the source.

此外,拥有代理导航路由器类中,可以直接在路由器中挂钩我们自己的行为。

Also, having a proxy to navigate in the Router class makes it easy to hook our own behaviour in the router directly.

至于为什么有时它不起作用可能是因为你试图在 this.router 没有的类中执行 this.router.navigate 存在。

As to why sometimes it doesn't work is probably because you're trying to do this.router.navigate in a class where this.router doesn't exist.

这篇关于Backbone.history.navigate和this.router.navigate之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:10