【问题标题】:how to get rid of typescript error message "Cannot find module 'localforage'"如何摆脱打字稿错误消息“找不到模块'localforage'”
【发布时间】: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


【解决方案1】:

我遇到运行时错误找不到模块“localforage”。我运行了命令

npm uninstall --save @ionic/storage
npm install --save @ionic/storage

对我有帮助

【讨论】:

    【解决方案2】:

    我已经尝试过typings installtsd install 命令。但他们都没有工作。最后我从离子论坛找到了解决方案:here

    如果有人遇到同样的问题,希望这会有所帮助。

    第 1 步:安装 Mozilla localForage。

    $ npm install localforage --save
    

    第 2 步:安装 localForage 类型定义。

    $ npm install -g typings
    $ typings install dt~localforage --save --global
    

    我没有将导入更改为import {localforage} from 'localforage';。就这样吧

    import LocalForage from 'localforage';
    

    【讨论】:

      【解决方案3】:

      我尝试为 localforage 安装类型,但对我不起作用。

      typings install localforage --ambient --save
      

      但我可以用 tds 做到这一点。

      npm i tsd -g
      tsd install localforage
      

      我使用的是 Ionic2 Beta.29 版

      更新 /typeings/index.d.ts

      /// <reference path="localForage/localForage.d.ts" />
      

      添加这一行。

      最后导入为

      import {localforage} from 'localforage';
      

      就是这样,现在一切正常。

      【讨论】:

      • 我没有/typings/index.d.ts
      • @nottinhill 如果您之前尝试过$ typings install localforage,您将获得/typings/index.d.ts 文件。
      【解决方案4】:

      首先 - 安装类型定义来自:localforage

      第二 - 将您的导入更改为:

      import {localforage} from 'localforage';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-21
        • 2014-11-21
        相关资源
        最近更新 更多