【问题标题】:How to Import JQuery iCheck to Angular 2/4如何将 JQuery iCheck 导入 Angular 2/4
【发布时间】:2017-05-25 11:22:42
【问题描述】:

我尝试在 Angular 4 中使用 iChek 库。但是当我在我的 TypeScript 文件中使用它时,我收到了如下错误:“类型 'JQuery' 上不存在属性 'iCheck'”

我的代码:

   import { Directive, ElementRef, HostListener, Input } from '@angular/core';

import * as $ from 'jquery';

@Directive({
  selector: '[icheck]'
})

export class IcheckDirective {
  constructor(el: ElementRef) {
     $(el).iCheck({
            checkboxClass: 'icheckbox_square-aero',
            radioClass: 'iradio_square-aero'
     })
  }
}

我想在我的组件中使用这个指令,比如:

 <label class="radio icheck-inline menu-label">
                <input type="radio" icheck name="filters.type" value="IN"> Entrant 
            </label>

有人有问题请解决?

【问题讨论】:

    标签: angular typescript angularjs-directive angular2-directives


    【解决方案1】:

    安装

    npm install jquery
    npm install -D @types/jquery
    npm install icheck
    npm install -D@types/icheck
    

    在“scripts”属性中将脚本添加到 /.angular-cli.json 文件中

    "scripts": [
      "../node_modules/jquery/dist/jquery.js",
      "../node_modules/icheck/icheck.js"
    ],
    

    在 /src/styles.css 文件中添加 icheck 皮肤

    @import '../node_modules/icheck/skins/square/blue.css';
    

    现在你可以在 typescript 中使用 icheck 库了

    import { Component, OnInit, OnDestroy } from '@angular/core';
    import { } from 'jquery';
    import { } from 'icheck';
    
    @Component({
      selector: 'app-login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.css']
    })
    export class LoginComponent implements OnInit {
      icheck: JQuery;
    
      constructor() {
      }
    
      ngOnInit() {
    
        // call iCheck
        const icheckOptions: ICheckOptions = {
          checkboxClass: 'icheckbox_square-blue',
          radioClass: 'iradio_square-blue',
          increaseArea: '20%'
        };
        this.icheck = jQuery('input').iCheck(icheckOptions);
      }
    }
    

    【讨论】:

      【解决方案2】:

      首先使用 npm 安装以下内容。

      npm install jquery
      npm install -D @types/jquery
      npm install icheck
      

      之后,您应该在 .angular-cli.json 中包含 iCheck javascript 文件

       "scripts": [
              "../node_modules/jquery/dist/jquery.js",
               "../node_modules/icheck/icheck.js"]
      

      希望这能解决您的问题

      【讨论】:

      • 感谢您的回复.. 是的,我安装了它们.. 我在我的 .angular-cli.json 中添加了,但没有工作..;在 ts 文件中,当我写道:constructor(el: ElementRef) { $(el).iCheck({..}) 我在 iCheck 中有一个红色标记,他说:“在类型 'JQuery' 上不存在属性 'iCheck'”..
      • 你导入icheck了吗import 'iCheck'
      • 好的,我导入了它,并在我的 typings.d.ts 中添加了 interface JQuery { iCheck(options?: any, callback?: Function) : any; } 。而且我现在没有红色标记..但是当我运行应用程序时,我得到Failed to compile. Property 'iCheck' does not exist on type 'JQuery'.
      • 嗯..这一切都需要。无论如何我会检查
      • 谢谢 .. 例如在这个网站上,他解释了如何将 JQuery 集成到 TypeScript 中......但是当我用 iChek 尝试它时......它不行medium.com/@NetanelBasal/…
      【解决方案3】:

      试试这个

      declare var $: any;
      
      @Directive({
        selector: '[icheck]'
      })
      
      export class IcheckDirective {
          $: any = $;
          constructor(el: ElementRef) {
               this.$(el.nativeElement).iCheck({
                  checkboxClass: 'icheckbox_square-aero',
                  radioClass: 'iradio_square-aero'
              })
          }
      }
      

      【讨论】:

      • 非常感谢hhhhh
      猜你喜欢
      • 1970-01-01
      • 2018-01-26
      • 2019-09-21
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 2018-04-23
      相关资源
      最近更新 更多