1

angular2-seed プロジェクト (タイピングと webpack を使用) に localForage を追加する際に問題があります。タイピングとangular2は初めてです。

型付けコマンドを使用して、Typescript の定義をtypings.json

    "localforage": "registry:dt/localforage#0.0.0+20160316155526",

vendor.ts追加したファイルに

const localforage:LocalForage = require("localforage");

私のservice.tsファイルでは、私のIDEはインポートを解決でき、オートコンプリートを行います

import {localforage} from "localforage";

this.store = localforage.createInstance({
  name: "products"
})

しかし、アプリケーションを実行すると、インポートされた localforage はundefined

ORIGINAL EXCEPTION: TypeError: Cannot read property 'createInstance' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'createInstance' of undefined
    at new SearchService (eval at <anonymous> (http://localhost:3000/app.bundle.js:36:2), <anonymous>:20:53)
    at eval (eval at <anonymous> (http://localhost:3000/vendor.bundle.js:189:2), <anonymous>:13:47)
    at Injector._instantiate (eval at <anonymous> (http://localhost:3000/vendor.bundle.js:141:2), <anonymous>:777:27)
    at Injector._instantiateProvider (eval at <anonymous> (http://localhost:3000/vendor.bundle.js:141:2), <anonymous>:715:25)
    at Injector._new (eval at <anonymous> (http://localhost:3000/vendor.bundle.js:141:2), <anonymous>:704:21)
    at InjectorInlineStrategy.getObjByKeyId (eval at <anonymous> (http://localhost:3000/vendor.bundle.js:141:2), <anonymous>:247:33)
    at Injector._getByKeyDefault (eval at <anonymous> (http://localhost:3000/vendor.bundle.js:141:2), <anonymous>:921:37)
    at Injector._getByKey (eval at <anonymous> (http://localhost:3000/vendor.bundle.js:141:2), <anonymous>:867:25)
    at Injector._getByDependency (eval at <anonymous> (http://localhost:3000/vendor.bundle.js:141:2), <anonymous>:853:25)
    at Injector._instantiate (eval at <anonymous> (http://localhost:3000/vendor.bundle.js:141:2), <anonymous>:743:36)

何か提案はありますか?, ありがとう!

4

3 に答える 3

2

systemjs に指示するのを忘れたようです (angular2 を使用しているため、ローダーを想定しています) localforage とは何か、どこからロードするか。このような何かがうまくいくはずです:

System.config({
 .....
  paths: {
    localforage: 'path/to/localforage/dist/localforage'
  }
 .....
});

System.defaultJSExtensions = true;

お役に立てれば

于 2016-04-03T09:22:58.150 に答える
0

export最後に、スクリプトの読み込みの問題ではありませんでしたが、キーワードを追加するのを忘れていましたvendor.ts

export const localforage:LocalForage = require("localforage");
于 2016-04-03T19:48:59.747 に答える
0

型付けはコンパイル専用であり、ランタイム用ではありません。そのため、エントリ HTML ファイルで SystemJS を構成する必要があります。

System.config({
  map: {
    localforage: '/path/to/localforage.js'
  },
  (...)
});

この方法で localforage モジュールをインポートできます:

import localforage from 'localforage';
于 2016-04-03T09:27:30.453 に答える