首页 > 解决方案 > 如何将 html 文件加载到 React 中的 iframe 元素中?

问题描述

我想将本地 html 文件加载到 React 中的iframe元素中。我在这个问题上遵循了公认的答案,但它不起作用。

我项目中的文件夹结构如下:

iframe 元素如下所示:

 <iframe
   src="../testHTMLFiles/index.html"
   height="100%"
   width="100%"
   frameBorder="0"
 />

我错过了什么?

标签: reactjsiframe

解决方案


iframe src 必须包含绝对 url:

https://www.example.com/testHTMLFiles/index.html

这是因为在您的构建中,文件在构建后在此位置不可用,并且 iframe 路径在 iframe 被实例化时被解析。


推荐阅读