名称'$'在当前作用域中不存在 - 模块内的typescript和jquery

时间:2013-04-02 22:48:57

标签: javascript jquery typescript

尝试在类或模块中使用jquery时出现错误:

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

element: jQuery; // all is good
elementou: $;    // all is fine



class buggers{
    private element: jQuery;   // The name jQuery does not exist in the current scope
    private elementou: $;      // The name $ does not exist in the current scope
}

module something {
    class buggers{
        private element: jQuery;   // The name jQuery does not exist in the current scope
        private elementou: $;      // The name $ does not exist in the current scope
    }
}

我不知道如何解决这个问题。

2 个答案:

答案 0 :(得分:2)

您正在使用$jQuery,就好像它们是类型一样。从DefinitelyTyped获取d.ts,您要查找的类型为JQueryJQueryStatic

$jQuery应该已在d.ts文件中声明为vars:

declare var jQuery: JQueryStatic;
declare var $: JQueryStatic;

...但如果你觉得你需要在课堂上再次申报,你可能想尝试:

class buggers{
    private element: JQuery;   // JQuery object. You'll need to assign something to this before you can use it. e.g element = $('selector');
    private elementou: JQueryStatic;      // Reference to the $ JQuery Static object
}

答案 1 :(得分:1)

我看到你这样做了,但我忽略了这个重要的一步。请记住在TS文件的顶部包含定义参考。

/// <reference path="../../assets/js/libs/typedefs/jquery-1.8.d.ts" />