如何在 reactjs 中呈现完整的 html
How to render a full html in reactjs
我有一个带有元标记的 HTML 文件,header,body,来自服务器的字符串格式
现在我的疑问是如何在 reactjs 中打开那个完整的 html 因为我们已经在 reactjs 中有一个 master index.html 。当我尝试使用 dangerouslySetInnerHTML
时,它给出了一个空白页。
正如我在评论中提到的,您可以通过多种方式进行。
尽管我讨厌 iframe,但它可能是适合您的解决方案。
注意:此方案与 React 无关
import React from "react";
const html = `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>div{
background: red;
color: white;
}</style>
</head>
<body>
<div>Hello iframe</div>
</body>
</html>`
export default function App() {
return (
<div className="App">
<iframe srcDoc={html} title="my-iframe"></iframe>
</div>
);
}
查看实例:here
请阅读 MDN 中的 iframe here 了解更多配置
更多关于 polyfill 的阅读 srcDoc
:here
我有一个带有元标记的 HTML 文件,header,body,来自服务器的字符串格式
现在我的疑问是如何在 reactjs 中打开那个完整的 html 因为我们已经在 reactjs 中有一个 master index.html 。当我尝试使用 dangerouslySetInnerHTML
时,它给出了一个空白页。
正如我在评论中提到的,您可以通过多种方式进行。 尽管我讨厌 iframe,但它可能是适合您的解决方案。
注意:此方案与 React 无关
import React from "react";
const html = `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>div{
background: red;
color: white;
}</style>
</head>
<body>
<div>Hello iframe</div>
</body>
</html>`
export default function App() {
return (
<div className="App">
<iframe srcDoc={html} title="my-iframe"></iframe>
</div>
);
}
查看实例:here
请阅读 MDN 中的 iframe here 了解更多配置
更多关于 polyfill 的阅读 srcDoc
:here