我想在我的angluar cli 项目中使用jQuery。
我已经尝试过这里的说明:https://www.gurustop.net/blog/2016/11/18/3821/jquery-angular2-angularcli-how-to
npm install --save jquery;
npm install --save-dev @types/jquery
angular-cli.json 文件。
"scripts": [
"../node_modules/jquery/dist/jquery.min.js"
]
import { Component } from '@angular/core';
import * as jQuery from 'jquery';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'app';
test = jQuery("body");
}
但是,当执行
ng build
时,我收到以下错误:ERROR in ./src/app/app.component.ts (2,25): Module ''jquery'' resolves
to a non-module entity and cannot be imported using this construct.
如果我删除
import * as jQuery from 'jquery';
和 `ng build',我会得到:ERROR in ./src/app/app.component.ts (10,10): Cannot find name 'jQuery'.
不知道从这里去哪里。
更新 1
基于@Saravana 发布的内容。
如果我只执行
declare const $: JQueryStatic
和 ng build
,则会收到错误消息:ERROR in ./src/app/app.component.ts (2,19): Cannot find name 'JQueryStatic'.
但如果我在他删除之前按照 Saravana 在他的第一篇文章中的建议去做。
import "jquery";
declare const $: JQueryStatic;
它毫无怨言地构建。所以我认为 Saravana 你应该恢复你的帖子。
最佳答案
由于 jQuery 库添加了全局变量 $
和 jQuery
,您可以只声明变量:
import "jquery";
declare const $: JQueryStatic;
关于jquery - 无法将 jQuery 添加到 angular cli 项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44593198/