繁体   English   中英

角度为 6 的 CamanJs

[英]CamanJs with angular 6

我正在尝试将 camanjs 与 angular 6 一起使用。如何添加 js lib 并在 npm 上没有类型时将其与 angular 一起使用。 我按照很多步骤

  • 使用 npm 安装 caman

  • 使用其在节点模块中的路径将其作为脚本添加到 angular.json

  • 将它导入到组件中

import * as Caman from 'path to caman in node module'

  • 在 AfterViewInit 我像这样使用它
 ngAfterViewInit() { Caman('#image-id', function () { this.brightness(10); this.contrast(20); }); }

但是当启动服务器时我收到这个错误

在此处输入图片说明

代码链接: https : //stackblitz.com/edit/angular-d5g941

.angular-cli.jsonscripts数组中,添加caman的路径

"scripts": [
    "../node_modules/path-to-caman"
],

angular.jsonscripts数组中的 Angular 6 的情况下,将路径添加到caman

"scripts": [
    "node_modules/path-to-caman"
],

然后,在你的组件中,像这样声明一个名为Caman的变量:

declare var Caman: any;

不需要

import * as Caman from 'path-to-caman'

因为你的编译器会抛出一个错误,指出:

找不到模块“卡门”。

只需将脚本添加到.angular-cli / angular.json的脚本数组(在 Angular 6 的情况下)就会将其添加到全局范围,您将能够从您的组件访问它。

此外,请确保在ngAfterViewInit生命周期钩子方法中调用任何函数,因为它尝试通过 id 访问 DOM 元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM