“Microsoft JScript运行时错误:‘$’在使用Typescript和jQuery时没有定义”。

时间:2022-11-07 18:25:16

I'm getting this error trying a simple jQuery sample with Typescript:

我正在尝试使用一个简单的jQuery样本来尝试这个错误:

"tsc.js(23915, 17) Microsoft JScript runtime error: '$' is undefined"

“tsc。(23915,17)

My entire .ts file:

我的整个.ts文件:

/// <reference path="scripts/jquery-1.8.d.ts" />

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

The jquery-1.8.d.ts is from DefinitelyTyped, but I get the same error if I use the standard jquery.d.ts. I'm not getting any errors on the reference tag, it's correct and is finding the .d.ts file.

jquery-1.8.d。ts是来自于定义类型,但是如果我使用标准的jquery.d.t则会得到相同的错误。我没有在参考标签上出错,这是正确的,找到了。d。ts文件。

I'm obviously missing something basic here, I can't figure out why I'm getting this error. I've got the VS 2012 extension installed and am getting full autocomplete on jQuery... so when I type '$' I do get autcomplete popups. The generated .js file is correct, nothing wrong there. A much more complex .ts file is compiling correctly and the output .js file is perfectly fine, so this is more of an annoyance than an error, I suppose. Or am I missing other errors because of this runtime error??

很明显我漏掉了一些基本的东西,我不知道为什么会有这个错误。我已经安装了VS 2012扩展,并在jQuery上实现了完全的自动完成……所以当我输入$时,我确实会得到自动完成弹出。生成的.js文件是正确的,没有问题。一个更加复杂的.ts文件正在正确编译,输出.js文件非常好,所以我认为这与其说是错误,不如说是麻烦。还是因为这个运行时错误而遗漏了其他错误?

I'm compiling by adding an "External Tool" in VS with command: C:\Program Files (x86)\Microsoft SDKs\TypeScript\0.8.1.1\tsc.exe arguments: -e "$(ItemPath)" --sourcemap

我编译通过添加一个“外部工具”在VS命令:C:\Program Files (x86)\Microsoft sdk \ \ 0.8.1.1 \ tsc手稿。exe参数:- e " $(ItemPath)"——sourcemap

4 个解决方案

#1


3  

I believe the issue is with the command that you are using itself, or at least one of the options you are passing in. "-e" tells the compiler to "Execute the script after compilation". So what you see, "Microsoft JScript runtime error: '$' is undefined", is not a compile error, but rather a runtime error that is showing up after the file is compiled and is in the process of being executed.

我认为问题在于您正在使用的命令本身,或者至少是您正在传递的其中一个选项。“-e”告诉编译器“编译后执行脚本”。因此,您看到的“Microsoft JScript运行时错误:'$'是未定义的”不是编译错误,而是在编译文件并正在执行的过程中出现的运行时错误。

Hope that helps!

希望会有帮助!

#2


2  

You need to include the real jquery.js in your HTML file, e.g.:

您需要包含真正的jquery。HTML文件中的js,例如:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript" src="myApp.js"></script>

#3


0  

-- Deleted my original answer..

——删除我原来的答案

Where exactly do you get the error : "tsc.js(23915, 17) Microsoft JScript runtime error: '$' is undefined" ?

你从哪里得到的错误是:“tsc”。(23915,17)微软JScript运行时错误:'$'未定义?

Also in this topic is a great outline of how to get the compiler to compile your .ts files on build : http://typescript.codeplex.com/discussions/403394 After that you can keep the build action of your .ts files on "TypeScriptCompile"

在本主题中,还有一个关于如何让编译器在构建时编译.ts文件的很好的大纲:http://typescript.codeplex.com/discussions/403394,之后您可以将.ts文件的构建动作保存在“TypeScriptCompile”中。

#4


0  

Didn't know we should not duplicate the answer... I have changed a little bit for my old answer. Ok, My solution is:

我不知道我们不应该重复这个答案……我已经改变了一些旧的答案。好的,我的解决方案是:

I am using VS 2015, and I am new to typescript.I used jQuery and leaflet in my project.

我正在使用VS 2015,我对打字稿很陌生。我在项目中使用了jQuery和单张。

  1. Download all these libraies from NuGet manager in VS 2015. “Microsoft JScript运行时错误:‘$’在使用Typescript和jQuery时没有定义”。

    下载所有这些图书馆从NuGet经理在VS 2015。

  2. Drag the library files (.js) as instructed in this tutorial:

    拖动库文件(.js),如本教程所示:

https://taco.visualstudio.com/en-us/docs/get-started-first-mobile-app/

https://taco.visualstudio.com/en-us/docs/get-started-first-mobile-app/

  1. Modify the index.html file by adding these lines.

    修改索引。通过添加这些行来创建html文件。

    <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" />
    <link rel="stylesheet" href="css/leaflet.css" />
    
    <script src="scripts/jquery-2.2.3.min.js"></script>
    <script src="scripts/jquery.mobile-1.4.5.min.js"></script>
    <script src="scripts/leaflet-0.7.3.min.js"></script>
    
  2. Modify the index.ts file. First add these lines on top to reference your libraries. You may need to change the path.

    修改索引。ts文件。首先在顶部添加这些行以引用库。您可能需要更改路径。

/// <reference path="jquery.d.ts"/>
/// <reference path="leaflet.d.ts"/>
  1. Add your own code within onDeviceReady(), otherwise you might get javascript runtime error, like sysmbol "$" is undefined. I guess this is because the codes try to load some function when the device is not ready yet. This has worked for me. Hope it would help you too.

    在onDeviceReady()中添加您自己的代码,否则您可能会得到javascript运行时错误,比如没有定义sysmbol“$”。我猜这是因为当设备还没有准备好时,代码试图加载一些功能。这对我很有效。希望这对你也有帮助。

        function onDeviceReady() {
    
        document.addEventListener('pause', onPause, false);
        document.addEventListener('resume', onResume, false);
    
    
        var parentElement = document.getElementById('deviceready');
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');
        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
    
        // your code goes in here
    
        }
    

#1


3  

I believe the issue is with the command that you are using itself, or at least one of the options you are passing in. "-e" tells the compiler to "Execute the script after compilation". So what you see, "Microsoft JScript runtime error: '$' is undefined", is not a compile error, but rather a runtime error that is showing up after the file is compiled and is in the process of being executed.

我认为问题在于您正在使用的命令本身,或者至少是您正在传递的其中一个选项。“-e”告诉编译器“编译后执行脚本”。因此,您看到的“Microsoft JScript运行时错误:'$'是未定义的”不是编译错误,而是在编译文件并正在执行的过程中出现的运行时错误。

Hope that helps!

希望会有帮助!

#2


2  

You need to include the real jquery.js in your HTML file, e.g.:

您需要包含真正的jquery。HTML文件中的js,例如:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript" src="myApp.js"></script>

#3


0  

-- Deleted my original answer..

——删除我原来的答案

Where exactly do you get the error : "tsc.js(23915, 17) Microsoft JScript runtime error: '$' is undefined" ?

你从哪里得到的错误是:“tsc”。(23915,17)微软JScript运行时错误:'$'未定义?

Also in this topic is a great outline of how to get the compiler to compile your .ts files on build : http://typescript.codeplex.com/discussions/403394 After that you can keep the build action of your .ts files on "TypeScriptCompile"

在本主题中,还有一个关于如何让编译器在构建时编译.ts文件的很好的大纲:http://typescript.codeplex.com/discussions/403394,之后您可以将.ts文件的构建动作保存在“TypeScriptCompile”中。

#4


0  

Didn't know we should not duplicate the answer... I have changed a little bit for my old answer. Ok, My solution is:

我不知道我们不应该重复这个答案……我已经改变了一些旧的答案。好的,我的解决方案是:

I am using VS 2015, and I am new to typescript.I used jQuery and leaflet in my project.

我正在使用VS 2015,我对打字稿很陌生。我在项目中使用了jQuery和单张。

  1. Download all these libraies from NuGet manager in VS 2015. “Microsoft JScript运行时错误:‘$’在使用Typescript和jQuery时没有定义”。

    下载所有这些图书馆从NuGet经理在VS 2015。

  2. Drag the library files (.js) as instructed in this tutorial:

    拖动库文件(.js),如本教程所示:

https://taco.visualstudio.com/en-us/docs/get-started-first-mobile-app/

https://taco.visualstudio.com/en-us/docs/get-started-first-mobile-app/

  1. Modify the index.html file by adding these lines.

    修改索引。通过添加这些行来创建html文件。

    <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" />
    <link rel="stylesheet" href="css/leaflet.css" />
    
    <script src="scripts/jquery-2.2.3.min.js"></script>
    <script src="scripts/jquery.mobile-1.4.5.min.js"></script>
    <script src="scripts/leaflet-0.7.3.min.js"></script>
    
  2. Modify the index.ts file. First add these lines on top to reference your libraries. You may need to change the path.

    修改索引。ts文件。首先在顶部添加这些行以引用库。您可能需要更改路径。

/// <reference path="jquery.d.ts"/>
/// <reference path="leaflet.d.ts"/>
  1. Add your own code within onDeviceReady(), otherwise you might get javascript runtime error, like sysmbol "$" is undefined. I guess this is because the codes try to load some function when the device is not ready yet. This has worked for me. Hope it would help you too.

    在onDeviceReady()中添加您自己的代码,否则您可能会得到javascript运行时错误,比如没有定义sysmbol“$”。我猜这是因为当设备还没有准备好时,代码试图加载一些功能。这对我很有效。希望这对你也有帮助。

        function onDeviceReady() {
    
        document.addEventListener('pause', onPause, false);
        document.addEventListener('resume', onResume, false);
    
    
        var parentElement = document.getElementById('deviceready');
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');
        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
    
        // your code goes in here
    
        }