代码之家  ›  专栏  ›  技术社区  ›  Oblomov

typescript错误:TS2304:找不到名称“$”

  •  6
  • Oblomov  · 技术社区  · 7 年前

     x = $('#msgBox_' + scope.boxId).position().left;
    

    生成一个

    错误TS2304:找不到名称“$”

    虽然 jquery @types 安装在node_modules文件夹中。

    我的tsconfig。json如下所示:

    {
      "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "sourceMap": true,
        "moduleResolution": "node",
        "declaration": true
      },
      "exclude": [
        "node_modules"
      ]
    }
    

    4 回复  |  直到 7 年前
        1
  •  8
  •   Brant    6 年前

    如果AngularCLI是注入JQuery的地方,那么在控制器/组件中,可以这样声明变量:

    declare var $: any;
    
        2
  •  8
  •   Aluan Haddad Vikrant Kashyap    6 年前

     import $ = require('jquery');
    

    尽管您应该深入研究es6模块和ts模块,以掌握如何利用es6模块化系统。还有d.ts文件。。。

    这应该有帮助: https://www.typescriptlang.org/docs/handbook/modules.html

        3
  •  7
  •   Oblomov    7 年前

    原来我缺少了“@types/jquery”节点模块,其中包含“$”的定义。一旦我在包中的“devdependences”列表中添加了“@types/jquery”:“^3.2.5”。json并通过“npm安装”重新安装,一切正常。

        4
  •  1
  •   Steffi Keran Rani J    5 年前

    node_模块 @types/jquery 缺少。

    我的解决方法如下:

    1. npm install --save @types/jquery .

      如果有人向你提出选择版本的问题,请选择最新版本。

    2. yarn install

    3. 一旦上述安装成功,请运行 yarn start 开始你的项目。