简体   繁体   English

模块解析失败:意外标记 (1:0) 您可能需要适当的加载程序来处理此文件类型。

[英]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.

相关问题 模块解析失败:意外的令牌(7:5)您可能需要适当的加载程序来处理此文件类型。 :Webpack,Bootstrap - Module parse failed: Unexpected token (7:5) You may need an appropriate loader to handle this file type. : Webpack, Bootstrap 模块解析失败:意外令牌 (41:41) 您可能需要适当的加载程序来处理此文件类型 - Module parse failed: Unexpected token (41:41) You may need an appropriate loader to handle this file type React/Zusand - 模块解析失败:意外的令牌。 您可能需要适当的加载程序来处理此文件类型 - React/Zustand - Module parse failed: Unexpected token. You may need an appropriate loader to handle this file type React - 模块解析失败:意外的令牌。 您可能需要适当的加载程序来处理此文件类型 - React - Module parse failed: Unexpected Token. You may need an appropriate loader to handle this file type Web3-react 错误模块解析失败:意外令牌您可能需要一个适当的加载程序来处理此文件类型。 web3-反应/钱包连接 - Web3-react Error Module parse failed: Unexpected token You may need an appropriate loader to handle this file type. web3-react/walletconnect Webpack 4:模块解析失败:意外字符“@”。 你可能需要一个合适的加载器来处理这种文件类型…… - Webpack 4: Module parse failed: Unexpected character '@'. You may need an appropriate loader to handle this file type… “npm run build”模块解析错误“您可能需要适当的加载程序来处理此文件类型。” - "npm run build" module parse error "You may need an appropriate loader to handle this file type." 您可能需要一个合适的加载器来处理这种文件类型。 ReactJs - You may need an appropriate loader to handle this file type. ReactJs Webpack 无法加载 pdf 文件:模块解析失败 您可能需要适当的加载程序来处理此文件类型 - Webpack Cannot load pdf file: Module parse failed You may need an appropriate loader to handle this file type 模块解析失败:您可能需要一个合适的加载器来处理此文件类型,目前没有配置加载器来处理此文件 - Module parse failed: You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM