我一直在尝试将localForage导入到我使用Aurelia-CLI创建的Aurelia TypeScript项目中。根据添加引用的文档,我已经尝试了很多方法。如果我只是在aurelia.json中的依赖项中引用它,那么库就会加载:
...
"localforage",
...
并导入它:
import * as localForage from "localforage";
唯一的问题是,我在编译时从TypeScript中获得了大量错误。用法如下:
localForage.getItem("user-settings").then((settings: UserSettings) => {
给我这样的错误:
src\repositories\settings-repository.ts(15,19): error TS2339: Property 'getItem' does not exist on type 'typeo
f "localforage"'.
[08:10:03] gulp-notify: [Error running Gulp] Error: src\repositories\settings-repository.ts(15,19): error TS23
39: Property 'getItem' does not exist on type 'typeof "localforage"'.
src\repositories\settings-repository.ts(19,23): error TS2339: Property 'setItem' does not exist on type 'typeo
f "localforage"'.
然后在运行时继续工作。我的typings.json中定义了localforage:
"localforage": "registry:dt/localforage#0.0.0+20160629074517"
TypeScript似乎认为这实际上存在于localForage.localforage中,它在运行时抛出一个未定义的异常,但没有编译时错误。我试过这样引用它:
import { localforage } from "localforage";
和(根据https://github.com/localForage/localForage的文件)
const localForage:LocalForage = require("localforage");
两者都不再出现编译错误,但在运行时不起作用。
答案 0 :(得分:0)
您的第一次拍摄大部分都在那里,但在查看[{3}}类型时,您可以看到正确的语法为getItem<T>
。所以试试这个:
localForage.getItem<UserSettings>("user-settings")