angular - 如何通过 angular-cli 将 Cropper.js 用于 Angular2 项目?

标签 angular typescript angular-cli cropper

我想使用 cropper.js ( https://github.com/fengyuanchen/cropperjs ) 通过 angular-cli 创建一个 Angular2 裁剪器。

我的指令是:

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);
  }
}

ng serve 的错误是:Cannot find module 'cropperjs'

但是 cropperjs 在 node_modules 中并且我的 angular-cli.json 更新为:

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

我没有任何解决问题的想法..

最佳答案

我不认为 cropper 有打字 所以当你 import Cropper from 'cropperjs'; 它不起作用

简单的解决方案是在 typings.d.ts 中添加

declare var Cropper: any;

它将让 typscript 以动态方式与 cropper 一起工作

删除你的导入

正确的解决方案是导入 install typings

npm install --save @types/cropperjs

关于angular - 如何通过 angular-cli 将 Cropper.js 用于 Angular2 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841045/

相关文章:

angular - http provider Observable.toPromise() 在 promise 链中没有按预期工作

javascript - 如何禁用多选下拉列表中的 2 个选项并使该选项变灰

typescript - 这在 Jasmine 测试 : Uncaught TypeError: Object prototype may only be an Object or null: undefined? 中意味着什么

angular - 构建具有多个配置的应用程序

javascript - 在Angular 8的新选项卡中打开url时如何使用状态传递数据

angular - ngrx 测试方法调度 Action

typescript - 编译器无法识别自定义 TS 类型

javascript - Angular - 将两个可观察结果合并为一个

angular-cli - Angular 13 baseHref 不再有效

angular - 如何使用 angular-cli 构建生成多个 html 文件?