[英]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'
ngAfterViewInit() { Caman('#image-id', function () { this.brightness(10); this.contrast(20); }); }
但是當啟動服務器時我收到這個錯誤
在.angular-cli.json
的scripts
數組中,添加caman
的路徑
"scripts": [
"../node_modules/path-to-caman"
],
在angular.json
的scripts
數組中的 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 元素。
然后將caman文件夾下nodemodules_中的Caman.pack.js添加到assets中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.