[英]Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type. <!DOCTYPE html>
I am trying to import a static about.html page inside a react component but getting this error.我正在尝试在 React 组件中导入 static about.html 页面,但出现此错误。 I want to import multiple static HTML pages.我想导入多个 static HTML 页。
This is my component这是我的组件
import React, { Component } from 'react';
import Page from './about.html';
var htmlDoc = {__html: Page};
export default class MyComponent extends Component {
constructor(props){
super(props);
}
render(){
return (<div dangerouslySetInnerHTML={htmlDoc} />)
}}
If you want to include static html in ReactJS. You need to use html-loader plugin if you are using webpack to serve your react code.如果你想在 ReactJS 中包含 static html。如果你使用 webpack 来服务你的反应代码,你需要使用 html-loader 插件。
First of all you need to install html-loader plugin.首先你需要安装 html-loader 插件。 Open your terminal and run following command:打开终端并运行以下命令:
npm install --save-dev html-loader Then open your webpack configuration file and add following code: npm install --save-dev html-loader 然后打开你的 webpack 配置文件并添加以下代码:
{
modules: {
loaders: [
{ test: /\.html$/, loader: 'html-loader' }
]
}
}
Now, in your react code do following changes:现在,在您的反应代码中做以下更改:
var htmlContent = require('path/to/html/file.html');
export default function MyComponent() {
return (
<div dangerouslySetInnerHTML={ {__html: htmlContent} } />
);
}
That is it.这就对了。 Now you may use static html files to load your html files in react.现在您可以使用 static html 文件在 React 中加载您的 html 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.