jquery - $(...).Jcrop 不是函数。在 Angular 2 中使用

标签 jquery angular jquery-plugins

我使用 jcrop 和 Angular 2 但出现错误。

added into index.html

<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/2.0.4/js/Jcrop.js"></script>

问题 enter image description here

component

 onSelect(media: Media): void {
        this.isSelecting = true;
        this.mediaFile = media;
        jQuery(this.element).ready(function ($: JQueryStatic): void {
            $('#target').Jcrop({
                aspectRatio: 1,
                setSelect: [175, 100, 400, 300]
            }, function (): void {
                const jcrop_api = this;
                const thumbnail = this.initComponent('Thumbnailer', {width: 130, height: 130});
            });
        });
    }

Html

  <img src="{{mediaFile.url}}" id="target" class="max-width-500">

最佳答案

我得到了这样的解决方案,并发布在这里寻求帮助

需要在组件中声明

 declare var $: JQueryStatic;

需要创建接口(interface)

interface JQuery {
  container: JQuery;
  Jcrop(...options: any[]): JQuery;
}

Jcrop 的方法

 private _initJCrop(): void {
        const componentInstance = this;
        this.jCrop = $('#crop-image').Jcrop(this.defaultJCropConfig, function(): void {
            componentInstance.jCropApi = this;
            componentInstance.jCropApi.container.on('cropmove', function(e: any, s: any, c: any): void {
                currentSelection = c;
                componentInstance._updateSelection(currentSelection);
            });
        });
        this.selectionX = 0;
        this.selectionY = 0;
        this.selectionWidth = this.minWidth;
        this.selectionHeight = this.minHeight;
        let currentSelection = {
            x: this.selectionX,
            y: this.selectionY,
            h: this.selectionHeight,
            w: this.selectionWidth
        };
    }

    private _updateSelection(currentSelection: any): void {
        this.selectionX = parseInt(currentSelection.x, 10);
        this.selectionY = parseInt(currentSelection.y, 10);
        this.selectionWidth = parseInt(currentSelection.w, 10);
        this.selectionHeight = parseInt(currentSelection.h, 10);
    }

    onCrop(): void {
        this.dialogRef.close({
            x: this.selectionX,
            y: this.selectionY,
            h: this.selectionHeight,
            w: this.selectionWidth
        });
    }

关于jquery - $(...).Jcrop 不是函数。在 Angular 2 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44299204/

相关文章:

javascript - 如何扩展 initComplete 数据表?

jquery - 使用已经设置的 id 的更优雅的方法是什么? jQuery ("#"+ t)?

javascript - 通过 Javascript 使 CSS 隐藏的区域淡入

javascript - JSP 页面中 javascript 函数参数的变量输入

javascript - 当在浏览器中重新加载 URL 到定义的路由器路由时出现 404

angular - 在 Rxjs 扫描运算符中,是否可以从扫描运算符创建的内部累加器中删除项目?

jquery - 如何在jplayer中添加播放列表

javascript - 如果图像尚未完全加载,插入加载器图标的最佳方法是什么?

javascript - 让脚本在没有 .text 的情况下工作

使用带有 Flex 的粘性柱的 Angular 6 mat-table 不起作用