无法导入lodash
我是新来的TypeScript和我在加载lodash时遇到问题。
这是我的代码:
///<reference path="../../typings/lodash/lodash.d.ts"/> ///<reference path="../interfaces/IScheduler.ts"/> import _ = require('lodash'); module MyModule { export class LeastUsedScheduler implements IScheduler { /// CODE HERE } }
我尝试通过以下方式replace导入行:
import * as _ from lodash;
在这两种情况下,我得到:
"error| Cannot find name 'IScheduler'."
当我删除导入指令时,它编译完美,但_在运行时未定义。
我也尝试把模块导入到模块中,但没有成功。
我很抱歉这一定是一个非常愚蠢的问题,但我无法弄清楚。
谢谢
编辑:
我明白了这个问题。 引用lodash的input在范围中创buildvariables_
。 这就是为什么编译没有import线罚款。 问题是引用input不真的导入lodash。 这就是为什么它在运行时失败。
当我导入lodash时,编译失败,因为lodash已经在范围之内。
感谢您的支持。
我不是100%的问题,但你可以尝试以下,让我知道它是怎么回事?
///<reference path="../../typings/lodash/lodash.d.ts"/> ///<reference path="../interfaces/IScheduler.ts"/> import _ = require("lodash"); export class LeastUsedScheduler implements IScheduler { doSomething(){ _.each([],function name(parameter) { // ... }); } }
编译时它看起来像:
var _ = require("lodash"); var LeastUsedScheduler = (function () { function LeastUsedScheduler() { } LeastUsedScheduler.prototype.doSomething = function () { _.each([], function name(parameter) { throw new Error("Not implemented yet"); }); }; return LeastUsedScheduler; })(); exports.LeastUsedScheduler = LeastUsedScheduler;
如果你导入模块import _ = require("lodash");
但你不使用它,TypeScript将删除导入(我为此添加了doSoemthing方法)。
更新:为什么它不工作?
问题是module
关键字是用来声明一个内部模块。 同时代码正在加载一个外部模块。 您应该避免混用内部和外部模块。 您可以通过http://www.codebelt.com/typescript/typescript-internal-and-external-modules/了解内部和外部模块之间的差异。
另外,如果您使用内部模块,请避免使用module
关键字,因为它已被弃用,您应该改为使用namespace
关键字。