4

尝试在类或模块中使用 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
    }
}

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

4

2 回答 2

2

您正在使用$并且jQuery好像它们是类型。取自DefinitiveTypedd.ts,您正在寻找的类型是或者JQueryJQueryStatic

$并且jQuery应该已经在d.ts文件中声明为变量:

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
}
于 2013-04-03T00:54:51.843 回答
1

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

/// <reference path="../../assets/js/libs/typedefs/jquery-1.8.d.ts" />
于 2013-04-27T03:17:28.530 回答