Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Cropper.js into Angular2 project with angular-cli?

I want use cropper.js (https://github.com/fengyuanchen/cropperjs) to create an Angular2 cropper with angular-cli.

My directive is:

import {Directive, ElementRef, Input, OnInit} from '@angular/core';
import Cropper from 'cropperjs';

@Directive({
  selector: '[ng2-cropper]'
})
export class Ng2CropperJsDirective implements OnInit {
  @Input('options') options: any = {};
  private cropper;
  constructor(private _element: ElementRef) { }

  ngOnInit(): void {
    console.log(Cropper);
    this.cropper = new Cropper(this._element.nativeElement, this.options);
  }
}

The error from ng serve is: Cannot find module 'cropperjs'

But cropperjs is in node_modules and my angular-cli.json is updated with:

"scripts": [
   "../node_modules/cropperjs/dist/cropper.js"
],

I do not have any ideas to solve the problem..

like image 295
Pierre-Luc BLOT Avatar asked Jan 24 '17 23:01

Pierre-Luc BLOT


1 Answers

I dont think that cropper has typings so when you do import Cropper from 'cropperjs'; it doesnot work

Simple solution would be in typings.d.ts add

declare var Cropper: any;

It will let typscript to work with cropper as dynamic

Remove import which you have

Right solution would be to import install typings

npm install --save @types/cropperjs
like image 116
Vova Bilyachat Avatar answered Oct 13 '22 22:10

Vova Bilyachat