yy499121268 2024-08-19 18:58 采纳率: 50%
浏览 2

最近在学习使用jointjs是遇到导包的问题

最近在学习使用jointjs是遇到导包的问题:Uncaught TypeError: Failed to resolve module specifier "@joint/core". Relative references must start with either "/", "./", or "../".
下面是两个详细的报错信息:

img


我的本地代码:
html:


```html
<!DOCTYPE html>


<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JointJS 示例</title>
    <!-- 引用外部样式文件 -->
    <link rel="stylesheet" href="demo01.css">
</head>
<body>
<!-- Paper 容器 -->
<div id="paper-container"></div>

<!-- 引用 JointJS 核心库 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.7.1/joint.min.js"></script>
<!-- 引用外部 JS 文件 -->
<script type="module" src="demo01.js"></script>
</body>
</html>


JS:

import { dia, shapes } from '@joint/core';

// 创建 Graph 模型
const graph = new dia.Graph();

// 创建 Paper 视图
const paper = new dia.Paper({
    el: document.getElementById('paper-container'),
    model: graph,
    width: '100%',
    height: '100%',
    gridSize: 10,
    drawGrid: true
});

// 创建两个矩形元素
const rect1 = new shapes.standard.Rectangle();
rect1.position(100, 30);
rect1.resize(100, 40);
rect1.attr({
    body: {
        fill: '#3498db'
    },
    label: {
        text: '矩形1',
        fill: '#ffffff'
    }
});

const rect2 = new shapes.standard.Rectangle();
rect2.position(300, 30);
rect2.resize(100, 40);
rect2.attr({
    body: {
        fill: '#e74c3c'
    },
    label: {
        text: '矩形2',
        fill: '#ffffff'
    }
});

// 将元素添加到 Graph 中
graph.addCells([rect1, rect2]);

// 创建一个连接两个矩形的连线
const link = new shapes.standard.Link();
link.source(rect1);
link.target(rect2);

// 将连线添加到 Graph 中
graph.addCell(link);

```
package.json:

img

不知道问题出在哪里,请帮忙看看,感谢!!s

  • 写回答

1条回答 默认 最新

  • 一直免费一直爽 2024-08-19 18:59
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    从你提供的信息来看,问题可能出在模块导入的路径上。在你的HTML文件中,你使用了<script type="module" src="demo01.js"></script>来引入外部JS文件。这意味着你需要使用ES6模块语法来导入JointJS库。

    请尝试将你的demo01.js文件中的导入语句更改为:

    import * as joint from 'jointjs';
    

    或者,如果你只需要导入特定的部分,可以使用:

    import { Graph, Paper } from 'jointjs';
    

    确保你的demo01.js文件位于与HTML文件相同的目录中,或者根据你的项目结构调整导入路径。

    另外,你的package.json文件中的依赖项似乎有误。你应该使用@joint/core而不是ojoint/core。请更新package.json文件,如下所示:

    {
      "name": "demo02",
      "version": "1.0.0",
      "description": "",
      "main": "demo01.js",
      "type": "module",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "private": true,
      "dependencies": {
        "@joint/core": "^4.0.4"
      }
    }
    

    然后,确保你已经安装了@joint/core依赖项。你可以使用以下命令安装:

    npm install @joint/core
    

    希望这些建议能帮助你解决问题。如果问题仍然存在,请随时提问。

    评论

报告相同问题?

问题事件

  • 修改了问题 8月19日
  • 创建了问题 8月19日

悬赏问题

  • ¥15 Ios抖音直播的时候如何添加自定义图片在直播间!
  • ¥15 ssh登录页面的问题
  • ¥60 渗透一个指定银行app,拿到客户信息,需要什么级别
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥15 stm32 串口通讯过程中的问题
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名