繁体   English   中英

SystemJS在Angular 2应用中找不到JQuery

[英]SystemJS cannot find JQuery in Angular 2 app

我将jQuery集成到了Angular 2应用中,如下所示:

typings install dt~jquery --save --global

我可以看到打字类型.json进行了相应的调整,但是通过npm start运行应用程序时,我收到此错误消息:

GET http://localhost:3000/jquery 404 (Not Found)

Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/jquery

Error: XHR error (404 Not Found) loading http://localhost:3000/jquery

任何想法如何解决这个问题?

我的index.html中确实有JQuery作为依赖项:

 <!-- Latest compiled and minified JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

更新:我按照Igor的建议进行了操作:我删除了types.json中的条目,并将依赖项添加到package.json中。 我删除了node_modules的全部内容,然后再次运行npm install 我仍然遇到这些编译错误:

 node_modules/@types/jquery/index.d.ts(623,5): error TS2374: Duplicate string index signature.
 node_modules/@types/jquery/index.d.ts(2872,5): error TS2374: Duplicate string index signature.
node_modules/@types/jquery/index.d.ts(2873,5): error TS2375: Duplicate number index signature.
node_modules/@types/jquery/index.d.ts(3246,5): error TS2300: Duplicate identifier 'export='.
typings/globals/jquery/index.d.ts(3224,5): error TS2300: Duplicate identifier 'export='.

我不知道如何/为什么应该有重复的类型定义。 这是我的package.json最近的样子:

"dependencies": {
"@angular/common": "~2.4.0",
"@angular/compiler": "~2.4.0",
"@angular/core": "~2.4.0",
"@angular/forms": "~2.4.0",
"@angular/http": "~2.4.0",
"@angular/platform-browser": "~2.4.0",
"@angular/platform-browser-dynamic": "~2.4.0",
"@angular/router": "~3.4.0",
"@angular/upgrade": "~2.4.0",
"bootstrap": "^3.3.7",
"chart.js": "^2.4.0",
"core-js": "^2.4.1",
"ng2-webstorage": "1.4.2",
"primeng": "^1.1.1",
"reflect-metadata": "^0.1.9",
"rxjs": "5.0.0-rc.4",
"systemjs": "0.19.41",
"zone.js": "^0.7.4",
"@types/jquery": "2.0.34"

如果您使用的是TypeScript 2版或更高版本

  • 从类型配置文件中删除jquery引用。 现在,最好使用npm引入类型定义。
  • 更新您的package.json文件,并添加"@types/jquery": "XXX",其中xxx是您使用的版本。 最新的是2.0.34( npm软件包 )。
  • 在您的打字稿中删除任何对jquery的import引用。
  • 只需引用jquery,就好像它已经被加载一样。 TypeScript应该自动获取JQuery的定义,而无需执行其他任何操作。

请注意,即使可能没有对定义文件的项目引用,TypeScript也会拾取定义文件。 只要定义文件在项目文件夹中任何位置的磁盘上,它就会被拾取。 这也意味着,如果以前使用过的typings文件中仍然存在较旧的定义文件,则应将其删除,否则在TypeScript编译时会出现编译错误。

暂无
暂无

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

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