无法将jQuery添加到角度cli项目

无法将jQuery添加到角度cli项目

问题描述:

我想在我的angluar cli项目中使用jQuery。 我在这里试图说明:https://www.gurustop.net/blog/2016/11/18/3821/jquery-angular2-angularcli-how-to无法将jQuery添加到角度cli项目

  1. 安装jQuery的

    npm install --save jquery; 
    
  2. 安装jQuery的类型认定中进行类型检查。

    npm install --save-dev @types/jquery 
    
  3. 在 “脚本” 数组添加的jquery文件的refenece内部 角cli.json文件。在我app.component.ts

    "scripts": [ 
        "../node_modules/jquery/dist/jquery.min.js" 
    ] 
    
  4. 导入jQuery的文件

    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'. 

不知道该从哪里出发。

更新基于什么@Saravana公布1

。 如果我只是做declare const $: JQueryStaticng 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; 

升级到最新的CLI,创建一个新的项目,并动了我的位和说明上述概述工作得很好。

npm uninstall -g angular-cli 

npm cache clean 

npm install -g @angular/[email protected]