【发布时间】:2016-07-25 07:55:05
【问题描述】:
我正在做一个 ionic 2/cordova 项目,并且正在使用带有 typescript 支持的 vim。
我的代码中有以下行:
import * as localforage from "localforage";
代码运行没有问题,但打字稿编译器给出以下错误
/path/to/file.ts|2 col 30 error| Cannot find module 'localforage'.
任何提示如何避免该错误?
更新
import {Injectable} from "angular2/core";
import * as localforage from "localforage";
@Injectable()
export class DbService {
constructor() {
//this.run();
}
setKeyVal(k, v){
console.log("db service setKeyVal");
let ran = Math.floor(Math.random() * 1000) + 1;
localforage.setItem(k, device.uuid).then(function () {
return localforage.getItem(k);
}).then(function (value) {
console.log(value);
// we got our value
console.log(navigator.connection.type);
console.log("setItem then");
}).catch(function (err) {
// we got an error
console.log("setItem catch");
});
}
}
更新 1 - typings.json
{
"dependencies": {},
"devDependencies": {},
"ambientDependencies": {
"cordova-ionic": "registry:dt/cordova-ionic#0.0.0+20160316155526",
"es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#4de74cb527395c13ba20b438c3a7a419ad931f1c",
"localforage": "registry:dt/localforage#0.0.0+20160316155526"
}
}
更新 2 - tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"filesGlob": [
"**/*.ts",
"!node_modules/**/*"
],
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
],
"compileOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
【问题讨论】:
-
嘿,谢谢小费。我现在通过
typings install --ambient --save localforage添加了 localforage 类型,现在我收到了错误error| Property 'setItem' does not exist on type 'typeof "localforage"'我做错了什么? -
你能把错误的代码贴出来吗?
-
我更新了未决问题
-
我已将我的建议作为答案发布
标签: cordova vim typescript ionic-framework ionic2