Html5History触发两次导航事件

Google Closure - Html5History fires NAVIGATE event twice

本文关键字:两次 导航 事件 Html5History      更新时间:2023-09-26

为什么google .history。Html5History对象火灾google .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);

这是log:

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

UPD:我发现在回调中e对象的isNavigation字段有两个不同的值。第一次取false值,第二次取true值。isNavigation意味着:

isNavigation如果事件是由浏览器操作触发的,则为True如向前或向后,点击链接,编辑URL或调用window.history。(去| |转发)。如果令牌已更改,则为False通过setToken或replaceToken调用。

但是怎样才能只解雇一个人呢?

我遇到了同样的问题。但在我的情况下,这两个事件都有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

我有一些问题…

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

init可能会发生两次?