尝试在类或模块中使用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
}
}
我不知道如何解决这个问题。
答案 0 :(得分:2)
您正在使用$
和jQuery
,就好像它们是类型一样。从DefinitelyTyped获取d.ts
,您要查找的类型为JQuery
或JQueryStatic
$
和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" />