javascript - 正确导入 JointJS v3
问题描述
使用 JointJS 的 2.2.1 版本时,我将其导入如下:
import { default as joint } from "jointjs";
现在我正在使用 3.0.2 版。上一行中的“关节”是未定义的。导入不再起作用。我在 JointJS 3.0.0 的发行说明中注意到:
显着变化 - 完全支持 ES 模块
我现在应该如何导入它?
解决方案
不再有默认导入,
import * as joint from 'jointjs'
工作得很好。如果需要更小的捆绑包,您可以挑选您实际需要的部件:
import { dia } from 'jointjs/src/core.mjs';
// import shapes you need
import * as standard from 'jointjs/src/shapes/standard.mjs';
const graph = new dia.Graph([], { cellNamespace: { standard } });
new dia.Paper({
cellViewNamespace: { standard },
el: document.getElementById('paper'),
width: 500, height: 500,
model: graph
});
const rectangle = new standard.Rectangle().size(200, 200).position(100, 100).addTo(graph)
请注意,您需要注意此设置中的cellViewNamespace
fordia.Paper
和cellNamespace
选项dia.Graph
。否则,您可能会遇到Uncaught Error: dia.ElementView: markup required error
运行此代码段可以快速检查您是否正确设置了命名空间:
const cells = JSON.stringify(graph.toJSON());
graph.clear();
graph.fromJSON(JSON.parse(cells));
推荐阅读
- python - 我尝试使用 pip 安装 win32gui,但出现此错误。我正在使用 python 3.8.5,我也有最新版本的 pip
- c# - 在已排序的 5 GB 文件中快速搜索行
- python - 根据另一个数字给出一个数字的最短表达式的算法
- python - TypeError:添加的层必须是类Layer的实例。成立:
- javascript - 如何使用 JSON 格式使用节点 js 响应 MYSQL 选择结果
- mongodb - 如何将 BSON 时间戳从 Mongo 更改流转换为 javascript 中的日期?
- javascript - 基于代理名称的自定义帖子类型的 WP Meta 查询
- homebrew - 正方体 .tr 文件为空
- java - 对象动画师只工作到中间,而不是滚动的底部
- c++ - 如何在 C++ 中的类方法中正确释放动态分配的内存