【问题标题】:Property 'select2' does not exist on type 'JQuery<HTMLElement>'类型“JQuery<HTMLElement>”上不存在属性“select2”
【发布时间】:2018-03-08 18:51:51
【问题描述】:

我正在使用带有 select2 插件的 Angular 4。我已经为jqueryselect2 添加了@types。 vscode intellisense 显示为方法 select2 并没有抛出任何错误,但是 angular-cli 的编译失败。

【问题讨论】:

  • 我建议你不要使用依赖于 jQuery 的库,因为在 Angular 中没有它你也可以做同样的事情。如果您不想自己操作选择元素(这是一个非常好的做法),我建议您使用为 angular 内置的库(例如 ng-select,link)。如果您想使用当前的实现,可能您必须在组件中的所有导入之后声明 var $:any。
  • @macvg 即使在安装了 ng-select 之后,也没有任何改变。
  • @rajdeep26 你能解决这个问题吗?
  • @rajdeep26 请编辑您的问题并复制粘贴 angular-cli 错误。否则没有答案是正确的。
  • @macvag 角度 cli 错误是 TS2551: Property 'select2' does not exist on type 'JQuery&lt;HTMLElement&gt;'. Did you mean 'select'?

标签: angular typescript webpack angular-cli


【解决方案1】:

我遇到了类似的问题并通过导入解决了它:

import 'select2';

我之前安装的:

npm install select2 --save

一旦你这样做了,你就可以像往常一样通过 jQuery 使用这个库,你的 IDE 就不会抱怨了。

【讨论】:

  • 我错过了import 'select2',谢谢你救了我。
【解决方案2】:

就我而言,这是因为我导入了另一个库 (sweetalert2),但 jQuery 定义错误。

sweetalert2.d.ts 885 行:

/**
 * These interfaces aren't provided by SweetAlert2, but its definitions use them.
 * They will be merged with 'true' definitions.
 */

// tslint:disable:no-empty-interface

interface JQuery {
}

这覆盖了JQuery 的定义,结果错误(原本应该是JQuery&lt;T = HTMLElement&gt;)。删除后一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2020-02-16
    • 2020-04-08
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多