2016-04-03 42 views
1

我在将angularFind添加到我的angular2-seed项目(使用类型和webpack)时遇到问题。我对typings和angular2很陌生。在angular2/typescript项目中未定义的变量localforage

随着分型的命令,我添加了打字稿定义来typings.json

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

在我​​文件我加

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

在我service.ts文件我的IDE能够解决的进口和自动做完成

import {localforage} from "localforage"; 

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

bu T当我运行应用程序,进口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) 

任何建议?谢谢!

+0

看来你angular2应用简化版,找出你的service..ts'localforage'文件 –

回答

2

它看起来像你忘了指示systemjs(因为你正在使用angular2我假设你的加载器)什么是localforage和从哪里加载它。这样的事情应该做的伎俩:

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

System.defaultJSExtensions = true; 

希望这有助于

+0

我目前使用'webpack'作为装载机。我的项目是基于https://github.com/angular/angular2-seed –

0

分型仅适用于编译和不运行。所以,你需要在你进入HTML文件中配置SystemJS:

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

这样,你就可以导入localforage模块:

import localforage from 'localforage'; 
+0

我目前使用'webpack'作为加载程序。我的项目基于github.com/angular/angular2-seed –

0

最后,它不是一个脚本加载问题,但我忘了加上export关键字​​

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