【发布时间】:2021-02-23 05:49:10
【问题描述】:
我想加载一个包含脚本标签的静态 html 文件作为反应组件。我尝试了html-loader 方法,但这是将 html 文件转换为字符串,但该字符串未在 react DOM 中正确呈现。
下面是我的示例 html 文件。
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.16.1/vis.css" type="text/css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.16.1/vis-network.min.js"> </script>
<center>
<h1>Test</h1>
</center>
<!-- <link rel="stylesheet" href="../node_modules/vis/dist/vis.min.css" type="text/css" />
<script type="text/javascript" src="../node_modules/vis/dist/vis.js"> </script>-->
<style type="text/css">
#mynetwork {
width: 900;
height: 450;
background-color: #ffffff;
border: 1px solid lightgray;
position: relative;
float: left;
}
</style>
</head>
<body>
<div id = "mynetwork"></div>
<script type="text/javascript">
// initialize global variables.
var edges;
var nodes;
var network;
var container;
var options, data;
// This method is responsible for drawing the graph, returns the drawn network
function drawGraph() {
var container = document.getElementById('mynetwork');
// parsing and collecting nodes and edges from the python
nodes = new vis.DataSet([{"id": 0, "label": 0, "shape": "dot", "size": 5}, {"id": 17, "label": 17, "shape": "dot", "size": 5}, {"id": 56, "label": 56, "shape": "dot", "size": 5}, {"id": 65, "label": 65, "shape": "dot", "size": 5}, {"id": 68, "label": 68, "shape": "dot", "size": 5}]);
edges = new vis.DataSet([{"from": 0, "to": 17, "weight": 10.1990327835083}, {"from": 0, "to": 56, "weight": 11.132073402404785}, {"from": 0, "to": 65, "weight": 11.344696998596191}, {"from": 0, "to": 68, "weight": 10.370659828186035}, {"from": 0, "to": 76, "weight": 11.320157051086426}, {"from": 0, "to": 82, "weight": 10.778494834899902}, {"from": 0, "to": 94, "weight": 11.241415977478027}]);
// adding nodes and edges to the graph
data = {nodes: nodes, edges: edges};
var options = {
"configure": {
"enabled": false
},
"edges": {
"color": {
"inherit": true
},
"smooth": {
"enabled": false,
"type": "continuous"
}
},
"interaction": {
"dragNodes": true,
"hideEdgesOnDrag": false,
"hideNodesOnDrag": false
},
"physics": {
"enabled": true,
"stabilization": {
"enabled": true,
"fit": true,
"iterations": 1000,
"onlyDynamicEdges": false,
"updateInterval": 50
}
}
};
network = new vis.Network(container, data, options);
return network;
}
drawGraph();
</script>
</body>
</html>
在 webconfig.js 中我添加了以下代码
{
test: /\.html$/,
exclude: /node_modules/,
use: {loader: 'html-loader'}
},
这就是我在 react 中的渲染方式
import Page from "./Sample.html";
var htmlDoc = { __html: Page };
<div dangerouslySetInnerHTML={htmlDoc} />
问题是它没有显示图表。但我可以在页面顶部看到 <h1> 标签,但在图表上看不到。
如何解决这个问题?
任何帮助表示赞赏 谢谢
【问题讨论】:
-
30000 个字符 - 下次试试 minimal reproducible example ;)
-
@mplungjan 将示例 html 更新为更少的字符。