Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Typescript中使用JQUERY_Jquery_Typescript - Fatal编程技术网

在Typescript中使用JQUERY

在Typescript中使用JQUERY,jquery,typescript,Jquery,Typescript,我试图将JQUERY整合到Typescript中,但我遇到了困难,不知道如何克服它。请记住,我是一个新手,当谈到打字脚本(一直与它只有几个月的混乱),所以有一个机会,这个问题是很容易解决,但我不能找出它 我有一个非常简单的TS程序: /// <reference path="node_modules/@types/jquery/index.d.ts" /> function PrintName() { let name:any = $('#txtName').val();

我试图将JQUERY整合到Typescript中,但我遇到了困难,不知道如何克服它。请记住,我是一个新手,当谈到打字脚本(一直与它只有几个月的混乱),所以有一个机会,这个问题是很容易解决,但我不能找出它

我有一个非常简单的TS程序:

/// <reference path="node_modules/@types/jquery/index.d.ts" />

function PrintName()
{
    let name:any = $('#txtName').val();
    console.log(name);
}
然后将引用添加到index.d.ts,但我得到了相同的错误。我使用的是Visual Studio代码1.27、tsc 3.0.1和npm 3.5


谢谢

安装
@types/jquery
只提供jquery的类型信息;您仍然必须在运行时加载实现。使用单独的
标记是可以的。如果您想要生成一个包含代码以及它所依赖的以模块形式打包的库(如jQuery)的JavaScript文件,则需要使用模块绑定器,如Webpack、Rollup或Browserify;TypeScript编译器本身无法执行此操作。

添加依赖项:在CLI中键入

npm --save jquery
npm --save jquery-ui-bundle
在angular-cli.json中:

"scripts": [ 
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/jquery-ui-bundle/jquery-ui.js"], ...
然后在您的组件中使用:

declare var jQuery:any; // jQuery is the same as $

谢谢你的回复,我将不得不检查这些模块然后。谢谢
"scripts": [ 
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/jquery-ui-bundle/jquery-ui.js"], ...
declare var jQuery:any; // jQuery is the same as $