簡體   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