5

为什么每次更改片段时 goog.history.Html5History 对象都会触发 goog.history.EventType.NAVIGATE 事件两次?这是代码示例:

var history = goog.history.Html5History.isSupported() 
      ? new goog.history.Html5History()
      : new goog.History();
goog.events.listen(history, goog.history.EventType.NAVIGATE, function(e) {
      console.log(['navigation', e.target.getToken()]);
});
history.setEnabled(true);

这是日志:

["navigation", "!/properties/new"]
["navigation", "!/properties/new"]

UPD:我发现回调中有两个不同isNavigation的对象字段值。e第一次是false有价值的,第二次是true有价值的。isNavigation方法:

isNavigation 如果事件由浏览器操作(例如前进或后退、单击链接、编辑 URL 或调用 window.history.(go|back|forward))触发,则为真。如果令牌已被 setToken 或 replaceToken 调用更改,则为 False。

但是如何才能让只有一个甚至被解雇呢?

4

2 回答 2

1

我遇到了同样的问题。但就我而言,这两个事件都有isNavigation==true.

init = function() {
  var h = goog.history.Html5History.isSupported() ?
      new goog.history.Html5History()  : new goog.History();

  goog.events.listen(h, goog.history.EventType.NAVIGATE, navigationCallback);
  h.setEnabled(true);
};

navigationCallback = function(e) {
  console.log(e, e.token, e.isNavigation);
};

// And then:
h.setToken("example1");
h.setToken("example2");
// And click "back" button in browser

输出:

goog.history.Event "example1" false
goog.history.Event "example2" false
goog.history.Event "example1" true
goog.history.Event "example1" true
于 2012-04-12T22:22:15.130 回答
0

我有一些问题..

谷歌是否解雇了一次 http://closure-library.googlecode.com/git/closure/goog/demos/history1.html

也许 init 发生了两次?

于 2013-10-18T09:23:53.517 回答