[英]Open a static HTML in React project
I have a React-Typescript project structure which looks like this:我有一个 React-Typescript 项目结构,如下所示:
/src
/components
/navigation
Routes.tsx
Navbar.tsx
Footer.tsx
/main
Main.tsx
/error
403.html //Newly created static HTML
I want to create another error component which will be having a Static HTML.我想创建另一个错误组件,它将具有 Static HTML。 Since it has a lot of dependencies, it cannot be converted to Javascript and I would like to use such static HTML in my project.由于它有很多依赖项,因此无法转换为 Javascript ,我想在我的项目中使用这样的 static HTML 。
I need to be able to call the static using this: localhost:4321/error/403.html.我需要能够使用以下命令调用 static:localhost:4321/error/403.html。 And for this, I have tried calling in the Route like this:为此,我尝试像这样调用 Route:
<Route exact path="/error/403.html" render={() => {window.location.href="/src/error/403.html"}} />
But I am getting TypeScript error stating: (JSX attribute) render?: ((props: RouteComponentProps<any, StaticContext, unknown>) => React.ReactNode) | undefined
但我收到 TypeScript 错误说明: (JSX attribute) render?: ((props: RouteComponentProps<any, StaticContext, unknown>) => React.ReactNode) | undefined
(JSX attribute) render?: ((props: RouteComponentProps<any, StaticContext, unknown>) => React.ReactNode) | undefined
Is this the correct way invoking the static webpage?这是调用 static 网页的正确方法吗?
You can set your static HTML
as innerHTML
as below,您可以将 static HTML
设置为innerHTML
,如下所示,
const path = require("path");
const fs = require("fs");
const loadHTML = async () => {
const filePath = path.resolve(__dirname, 'relative', 'path', 'to', 'your', 'html');
const html = await fs.readFile(filePath, 'utf8');
return html;
}
<Route exact path="/error/403.html" render={() => <div dangerouslySetInnerHTML={{ __html: loadHTML()}}/> } />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.