javascript - 未找到模块:错误:无法解析...使用外部 JS 库时
问题描述
我正在尝试在我的 Angular2+ 应用程序中使用 WYSIWYG 编辑器 ( TUI Editor )。他们不提供 Angular 包装器,所以我决定创建自己的(基于此wrapper)。
由于使用 npm install 的一些问题,我将他们的脚本文件(https://cdnjs.cloudflare.com/ajax/libs/tui-editor/1.4.0/tui-editor-Editor-full.js)保存在一个文件夹中。
我创建了一个服务来创建编辑器的实例,它工作正常:
import {ElementRef, Injectable} from '@angular/core';
import Editor from '../js/tui-editor-Editor-full';
@Injectable({
providedIn: 'root'
})
export class TuiService {
editor: any;
createEditor(options: object, element: ElementRef): any {
if (options) {
this.editor = new TuiEditor(Object.assign({
el: element.nativeElement
},
options));
}
return this.editor;
}
...
}
现在我不想添加他们的扩展之一(表一:https ://github.com/nhn/tui.editor/blob/master/docs/using-extensions.md )但我面临一些问题。
当我以与添加编辑器脚本文件相同的方式添加扩展脚本时
import '../js/tui-editor-extTable';
我在构建我的应用程序时遇到了这些错误
WARNING in ./src/js/tui-editor-extTable.js
Module not found: Error: Can't resolve 'tui-editor' in 'C:\workspace\src\js'
WARNING in ./src/js/tui-editor-extTable.js
Module not found: Error: Can't resolve 'tui-editor/dist/tui-editor-Viewer' in 'C:\workspace\src\js'
ERROR in ./src/js/tui-editor-extTable.js
Module not found: Error: Can't resolve 'jquery' in 'C:\workspace\src\js'
ERROR in ./src/js/tui-editor-extTable.js
Module not found: Error: Can't resolve 'to-mark' in 'C:\workspace\src\js'
ERROR in ./src/js/tui-editor-extTable.js
Module not found: Error: Can't resolve 'tui-code-snippet' in 'C:\workspace\src\js'
i 「wdm」: Failed to compile.
如果您查看tui-editor-Editor-full.js,所有这些依赖项都已包含在此文件中。为什么tui-editor-extTable.js脚本看不到它们?
如何使用此扩展程序?
我将 jquery 安装为我的项目的依赖项,但tui-editor-extTable.js脚本似乎仍然无法识别它。
谢谢你的帮助
解决方案
我最终决定克隆 tui-editor 存储库以使用自定义 webpack 配置创建我自己的 tui-Editor js 文件。
这包括:
- 项目中使用的所有依赖项(jquery、highlightjs ...)
- 表扩展
所以我只需要在我的代码中导入这个文件。
这不是最好的解决方案,它没有回应主要问题,但它有效:/
推荐阅读
- typescript - 泛型类型作为接收参数的属性
- php - 如何知道输入数组元素是否为空?
- ios - 多个 UICollectionView 在同一布局中滚动
- python - 尝试导入 firebase 包时收到 .async 错误
- android - Android ListView 取消选择以前选择的项目
- python - msgpack 能否提供更好的性能和与 python 的 struct.pack() 相同的功能?
- javascript - javascript: document.write 函数已重新定义,我该如何恢复它
- android - 如何在 C++ 中使用 OpenSLES 在 Android 上播放两通道 wav 文件?
- python - 为什么我的 tensorflow 代理的训练变慢了?是因为我的批量喂养策略吗?
- angularjs - 如何在AngularJS中的两个控制器之间广播事件