【问题标题】:How to load external static HTML page with script tags in react?如何在反应中加载带有脚本标签的外部静态 HTML 页面?
【发布时间】: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} />

问题是它没有显示图表。但我可以在页面顶部看到 &lt;h1&gt; 标签,但在图表上看不到。

如何解决这个问题?

任何帮助表示赞赏 谢谢

【问题讨论】:

标签: html reactjs


【解决方案1】:

您可以使用 dangerouslySetInnerHTM 道具。这个问题已经在stackoverflow上得到了回答。看看React: how to load and render external html file? 这将解决您的问题

【讨论】:

  • 如果您看到我的问题,我已经在使用 dangerouslySetInnerHTML 并浏览了您指定的链接。问题是它没有读取&lt;script&gt; 标签中的内容。无论如何感谢您指出链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 2018-11-24
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
相关资源
最近更新 更多