如何将HTML文件嵌入到React组件中

时间:2019-02-11 21:35:48

标签: javascript html reactjs iframe

我正在尝试将本地html文件嵌入到react组件中。本地文件称为Test.html,存储在src文件夹中。

我正在使用iframe代码将其嵌入:

 <iframe id="Example2"
             title="iframe Example 2"
             width="500" height="300"
             src="../../Test.html"/>

Test.html是:

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <title>Test</title>
</head>
<body>
<div>Hello world</div>
</body>
</html>

页面呈现<iframe>标签时,呈现方式如下:

<iframe id="Example2" title="iframe Example 2" width="500" height="300" src="../../Test.html">
#document
<!doctype html>
<head> ... </head>
<body id="home"> ... </body>
</iframe>

头部和正文中的...是来自public / index.html文件的样板html,而不是src / Test.html。

我在网上搜索了说明,但一无所获。

如何获取我的Test.html文件以在我的react组件的<iframe>标签内进行渲染?

编辑:如果我将Test.html放在远程服务器上并从那里链接到它,那么它将很好地工作。

即,如果我制作了src="www.somesite.com/Test.html",则iframe会呈现正确的页面。

因此,问题显然出在加载本地资源上。

我认为存储Test.html的正确位置是我的项目src文件夹中。

0 个答案:

没有答案