reactjs - 反应图不可见并显示错误
问题描述
我尝试使用react-diagrams
库,但遇到了一些问题。
首先在一线
const link = port1.link<DefaultLinkModel>(port2);
它向我显示了一个错误
类型错误:link.addLabel 不是函数
并假设我删除了链接部分,代码运行,我没有收到任何错误,但网页上什么也看不到......
这是代码
应用程序.js
import React from "react";
import createEngine, {
DefaultLinkModel,
DefaultNodeModel,
DiagramModel
} from "@projectstorm/react-diagrams";
import { CanvasWidget } from "@projectstorm/react-canvas-core";
// create an instance of the engine with all the defaults
const engine = createEngine();
// node 1
const node1 = new DefaultNodeModel({
name: "Node 1",
color: "rgb(0,192,255)"
});
node1.setPosition(100, 100);
let port1 = node1.addOutPort("Out");
// node 2
const node2 = new DefaultNodeModel({
name: "Node 1",
color: "rgb(0,192,255)"
});
node2.setPosition(100, 100);
let port2 = node2.addOutPort("Out");
// link them and add a label to the link
const link = port1.link < DefaultLinkModel > port2;
link.addLabel("Hello World!");
const model = new DiagramModel();
model.addAll(node1, node2);
engine.setModel(model);
class App extends React.Component {
render() {
return <CanvasWidget engine={engine} />;
}
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from "./App"
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
serviceWorker.unregister();
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<title>React App</title>
</head>
<body>
<style>
.srd-diagram {
height: 100vh;
}
</style>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
请帮我整理一下...
解决方案
我知道这篇文章太旧了,但如果有人在苦苦挣扎,仍然会有所帮助。
如果要在节点之间添加链接,请尝试为link
.
const node1 = new DefaultNodeModel({
name: "Node 1",
color: "rgb(0,192,255)",
});
node1.setPosition(100, 100);
let port1 = node1.addOutPort("Out");
const node2 = new DefaultNodeModel({
name: "Node 2",
color: "rgb(0,100,255)",
});
node2.setPosition(100, 200);
let port2 = node2.addInPort("In");
const link = new DefaultLinkModel();
link.setSourcePort(port1);
link.setTargetPort(port2);
对于第二部分:
并假设我删除了链接部分,代码运行,我没有收到任何错误,但网页上什么也看不到......
您需要设置画布的高度和宽度以使其可见。
应用程序.js
<CanvasWidget className="canvas" engine={engine} />
应用程序.css
.canvas {
height: 400px;
width: 100%;
}
推荐阅读
- node.js - 节点快递:动态创建具有所有属性的全身链接?
- tomcat - 配置gsp网页后如何编译grails程序?
- reactjs - GraphQL:接受缺少请求字段的查询响应?
- swift - SwiftUI:更改动画进度条的开始位置(或一般的形状绘制)
- woocommerce - 在 URL 中不带 ?pay_for_order=true&key=wc_order_XXXXXX 参数支付 Woocommerce 订单
- python - 获取 sqlite3.OperationalError:输入不完整
- python - 如何从 linspace 中采样而不批量替换
- c# - 使用城堡为合同的命名端点创建 WCF 代理
- javascript - 组件重新渲染时数组为空
- javascript - PHP Datables 没有显示任何行 - 服务器端