【问题标题】:Importing lunr in typescript?在打字稿中导入lunr?
【发布时间】:2021-02-13 11:01:06
【问题描述】:

Defintely Typed repository 像这样导入它:

import * as lunr from 'lunr';

但是,当在 Stackblitz 中像这样使用它时,会出现以下错误:

lunr 不是函数

想法?这是 Stackblitz:

https://stackblitz.com/edit/typescript-lunr-test

【问题讨论】:

  • 你试过在本地运行吗?这可能是 StackBlitz 的错误
  • 是的,我尝试在 Angular 项目中本地运行它。它会产生错误。 gist.github.com/fireflysemantics/…

标签: javascript typescript typescript-typings lunrjs


【解决方案1】:

尝试使用:

import lunr from "lunr"

这种方式有效,因为 lunr.js 正在导出默认值,或者它用一个单一的东西覆盖导出对象。看看这篇文章的前 4-5 段:blog.atomist.com/typescript-imports

【讨论】:

  • 我编辑了你的答案并对这个问题投了赞成票。我会很高兴看到解释为什么它会起作用,而不是它过去的工作方式。
  • 是的,对不起,我对 JS 比对打字稿更熟悉。这种方法之所以有效,是因为 lunr.js 正在导出默认值,或者它用一个单一的东西覆盖了导出对象。看看本文前4-5段:blog.atomist.com/typescript-imports
【解决方案2】:

Definitely Typed 使用 older version of lunr,所以我尝试了不同的导入:import lunr from "lunr",这会引发错误,指出 posting[field]undefined (source code)。

然后我注意到Builder 中使用的"text" 字段未在index 中初始化。在index 中添加this.field("text") 导致页面加载没有错误。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 2023-02-09
  • 2012-12-26
  • 1970-01-01
  • 2018-05-22
  • 2017-07-31
  • 2021-07-23
相关资源
最近更新 更多