如何将`this`传递给typeahead.js事件

时间:2015-11-04 01:42:37

标签: javascript jquery reactjs typeahead.js react-router

我正在使用typeahead.js库在我的应用中进行搜索。我整理了以下搜索,但不幸的是,最后,我无法访问this。这是当前的代码:

$('#search-field').typeahead({hint:false, minLength: 2}, {
  configSetings: settings,
}).bind('typeahead:select', function(obj, datum) {
  this.history.pushState(null, `/term/${datum.permalink}`);
});

我试图将.bind(this)调用typeahead:select事件,但当我这样做时,我最终会收到一条错误消息:

  

未捕获的TypeError:m.push不是函数

我不知道如何解决这个问题。有任何想法吗?我正在使用ReactJS,Webpack和Typeahead.js。

1 个答案:

答案 0 :(得分:0)

这个怎么样:

$('#search-field').typeahead({hint:false, minLength: 2}, {
  configSetings: settings,
}).bind('typeahead:select', (function(obj, datum) {
  this.history.pushState(null, '', `/term/${datum.permalink}`);
}).bind(this));