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

标签 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事件,但当我这样做时,我最终会得到一个错误,其中指出:

Uncaught TypeError: m.push is not a function

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

最佳答案

这个怎么样:

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

关于javascript - 如何将 `this` 传递给 typeahead.js 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33512308/

相关文章:

javascript - React.js 中的样式表

javascript - 尝试在 p5.js 中调用多行字符串时出现 TypeError

jquery - firebug 中偶尔会显示 "Image corrupt or truncated"

php - Json数据奇怪的行为

javascript - 页面刷新后 ng-click 停止工作

javascript - 如何使用 react 过滤数据?

javascript - 如何在使用 Reactjs 时将数据写入 JSON 文件

javascript - jquery cycle2 幻灯片嵌套幻灯片下一个和上一个按钮

javascript - 如何在dimple.js中使用series.stack = false来抑制聚合;每个系列都有不同的符号

javascript - 我怎样才能让这个脚本运行多个单词