angular-croppie-module无法正确导入@ type / croppie

时间:2018-03-23 03:59:13

标签: angular5 croppie

我是角色5的新手,并努力在我的应用程序中实现angular-croppie-module。 我试图在模块github上得到一些答案,但没有成功。 看来croppie.directive.d.ts只设法导入'Croppie'命名空间而不是类。 因此,一旦我到达带有模块的页面,我就会

  

ERROR TypeError:__ WWEPACK_IMPORTED_MODULE_1_croppie ___ default.a不是构造函数

我创建了一个github repo来展示问题的简单演示 https://github.com/ErwinSanquer/croppie-demo

谢谢,

2 个答案:

答案 0 :(得分:2)

我正在使用angular4,发现croppie很容易单独使用,然后你可以使用croppie docs找出要调用的方法。我刚刚做了:

npm install croppie

然后创建了一个使用它的组件(这里是组件的简化版本):

import { Component, OnInit, OnDestroy, Output, EventEmitter, Input } from '@angular/core';
import { Croppie } from 'croppie/croppie'; // Reference: http://foliotek.github.io/Croppie/

@Component({
  selector: 'image-upload',
  templateUrl: './image-upload.component.html',
  styleUrls: ['./image-upload.component.scss']
})
export class ImageUploadComponent implements OnInit, OnDestroy {
  uploadElement: any;
  croppie: any;
  imageChanged: boolean = false;

  constructor() { }

  ngOnInit() {
    this.initCroppie();
    this.initFileUpload();
  }

  initCroppie() {
    const cropElement = document.getElementById('imageUploadItem');
    this.croppie = new Croppie(cropElement, {
      viewport: { width: 200, height: 200, type: 'circle' },
      boundary: { width: 200, height: 200 },
      enableOrientation: true
    });

    let preloadedImageUrl = Constants.DEFAULT_PROFILE_IMG_URL;

    this.croppie.bind({
      url: preloadedImageUrl,
      zoom: 0
    });
  }

  initFileUpload() {
    this.uploadElement = document.getElementById('fileUploadInput');
    this.uploadElement.addEventListener('change', this.onFileUpload.bind(this));
  }

  onFileUpload() {
    if (this.uploadElement.files && this.uploadElement.files.length > 0) {
      this.imageChanged = true;

      const file = this.uploadElement.files[0];
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = ((event: any) => {
        this.croppie.bind({
          url: event.target.result
        });
      });
    }
  }

  rotateLeft() {
    this.croppie.rotate(90);
    this.imageChanged = true;
  }

  rotateRight() {
    this.croppie.rotate(-90);
    this.imageChanged = true;
  }

  getCroppieImage(quality: number, numCalls: number): Promise<any> {
    return this.croppie.result({
      type: 'blob',
      size: {
        width: 500,
        height: 500
      },
      format: 'jpeg',
      quality: quality,
      circle: false
    })
      .then((imageDataBlob) => {
        // If image size is still too large just call result again with lower quality
        if (imageDataBlob.size > 500000 && numCalls < 3) {
          quality = quality - 0.1;
          numCalls++;
          return this.getCroppieImage(quality, numCalls);
        } else {
          return imageDataBlob;
        }
      });
  }

  ngOnDestroy() {
    this.uploadElement.removeEventListener('change', this.onFileUpload);
  }
}

确保模板有一个id为'imageUploadItem'的div,以供croppie使用,如果你让用户上传图像(就像我在上面的组件中所做的那样),请确保你输入的type = file with ID = 'fileUploadInput'。

答案 1 :(得分:0)

更新:

该问题已在版本0.0.6中修复 https://github.com/gatimus/angular-croppie/issues/4