简体   繁体   English

错误1找不到符号“ $”

[英]Error 1 Could not find symbol '$'

When I use Visual Studio to edit an external Javascript file and use Javascript within the page, it unsurprisingly is unaware of JQuery, because the web page that owns it hasn't loaded the Javascript file. 当我使用Visual Studio编辑外部Javascript文件并在页面中使用Javascript时,毫无疑问,它不知道JQuery,因为拥有它的网页尚未加载Javascript文件。

//test.js: //test.js:

$(document).ready(function () {
    alert("If you really need a code example");
});

As a result, I get this error: 结果,我得到这个错误:

Error   1   Could not find symbol '$'

Again, this is a design time error, not a runtime error. 同样,这是设计时错误,而不是运行时错误。

Is there a way to make the Visual Studio Editor a little smarter or at least suppress these errors? 有没有一种方法可以使Visual Studio Editor更加智能,或者至少抑制这些错误?

Update: 更新:

I think it was the Typescript editor that I was in that just complained that there was an error, no error is shown when editing the rresulting javascript file. 我认为这是我在其中的Typescript编辑器,只是抱怨有一个错误,在编辑重新生成的javascript文件时未显示任何错误。

Modified Question: 修改后的问题:

How can I make the TypeScript file editor happy? 如何使TypeScript文件编辑器满意? I think the "editor" may be Web Essentials add in and that there may be a directive to make it ignore the $, perhaps. 我认为“编辑器”可能是Web Essentials的补充,并且也许有一条指令使其可能忽​​略$。 Or am I thinking of JSLint? 还是我在想JSLint?

I''m new to all of this... 我是这一切的新手...

Try: 尝试:

jQuery(document).ready(function(){
});

or maybe you are missing the ); 或您可能错过了); after the } }

In TypeScript, all variables must be declared. 在TypeScript中,必须声明所有变量。

You can declare it with declare var $; 您可以使用declare var $;进行declare var $; at the beginning of the file. 在文件的开头。

Or you can use a definition file for jQuery: 或者,您可以将定义文件用于jQuery:

  1. Download it ; 下载 ;
  2. Then, in your .ts file, include the definition file: 然后,在您的.ts文件中,包括定义文件:

    /// <reference path='jquery.d.ts' /> /// <参考路径='jquery.d.ts'/>

    // here the compiler known jQuery //这里是编译器已知的jQuery

    $(document).ready(/* ... */); $(document).ready(/ * ... * /);

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

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