简体   繁体   English

角度为 6 的 CamanJs

[英]CamanJs with angular 6

I am trying to use camanjs with angular 6. How to add js lib and use it with angular when there are no types for it on npm.我正在尝试将 camanjs 与 angular 6 一起使用。如何添加 js lib 并在 npm 上没有类型时将其与 angular 一起使用。 I follow many steps我按照很多步骤

  • install caman using npm使用 npm 安装 caman

  • adding it to angular.json as a script using its path in node modules使用其在节点模块中的路径将其作为脚本添加到 angular.json

  • import it into the component like将它导入到组件中

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

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

but when start server I get this error但是当启动服务器时我收到这个错误

在此处输入图片说明

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

Inside .angular-cli.json 's scripts array, add the path to caman.angular-cli.jsonscripts数组中,添加caman的路径

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

In case of Angular 6 in angular.json 's scripts array, add the path to camanangular.jsonscripts数组中的 Angular 6 的情况下,将路径添加到caman

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

and then, inside your Component, just declare a variable named Caman like this:然后,在你的组件中,像这样声明一个名为Caman的变量:

declare var Caman: any;

You don't need to do不需要

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

as your compiler will throw an error stating that:因为你的编译器会抛出一个错误,指出:

Cannot find module 'caman'.找不到模块“卡门”。

Just adding the script to the scripts array in .angular-cli / angular.json (in case of Angular 6) will add it to the global scope and you'll be able to access it from your components.只需将脚本添加到.angular-cli / angular.json的脚本数组(在 Angular 6 的情况下)就会将其添加到全局范围,您将能够从您的组件访问它。

Also, make sure that you call any function on it inside the ngAfterViewInit lifecycle hook method as it tries to access the DOM elements by id.此外,请确保在ngAfterViewInit生命周期钩子方法中调用任何函数,因为它尝试通过 id 访问 DOM 元素。

  • First do as How can i use foxTooltip with Angular?首先做我如何将foxTooltip与Angular一起使用?

  • Then add Caman.pack.js from nodemodules _ under caman folder into assets然后将caman文件夹下nodemodules_中的Caman.pack.js添加到assets中

  • Make sure your declared var in the beginning of the Component is the Same name as in Package "Caman" case senstive确保您在组件开头声明的 var 与包“Caman”中的名称相同,区分大小写

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

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