gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:08:32 26 4
gpt4 key购买 nike

我想在我的 Angluar CLI 项目中使用 jQuery。我已经尝试过这里的说明:https://www.gurustop.net/blog/2016/11/18/3821/jquery-angular2-angularcli-how-to

  1. 安装 jQuery

    npm install --save jquery;
  2. 安装 jQuery 类型定义以进行类型检查。

    npm install --save-dev @types/jquery
  3. 在"scripts"数组里面添加jquery文件的refeneceangular-cli.json 文件。

    "scripts": [
    "../node_modules/jquery/dist/jquery.min.js"
    ]
  4. 在我的 app.component.ts 文件中导入 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'.

不知道从这里去哪里。

更新 1

基于@Saravana 发布的内容。如果我只是执行 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;

关于jquery - 无法将 jQuery 添加到角度 cli 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44593198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com