简体   繁体   English

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

[英]The name '$' does not exist in the current scope - typescript and jquery inside a module

Im getting an error when trying to use jquery inside a class or module: 尝试在类或模块中使用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
    }
}

I have no idea how to fix this. 我不知道如何解决这个问题。

You're using $ and jQuery as if they were types. 你正在使用$jQuery ,就好像它们是类型一样。 Taking the d.ts from DefinitelyTyped , the type you are looking for is either JQuery or JQueryStatic DefinitelyTyped获取d.ts ,您要查找的类型是JQueryJQueryStatic

$ and jQuery should already be declared vars in the d.ts file: $jQuery应该已经在d.ts文件中声明了vars:

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

... but if you feel you need to declare them again in your class, you might want to try: ...但如果你觉得你需要在课堂上再次申报,你可能想尝试:

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
}

I see that you did this, but I had overlooked this important step. 我看到你这样做了,但我忽略了这个重要的一步。 Remember to include the definition reference at the top of your TS file. 请记住在TS文件的顶部包含定义参考。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM