如何修复 React 的"Refused to execute script because its MIME type ('text/html') is not executable, and



我的 React 应用程序不会从我的本地文件夹加载以下脚本。我将 webpack 更新到 4.0 版,每次尝试加载脚本时,都会出现以下错误:

拒绝从"http://localhost:8080/components/homepage/texting.js"执行脚本,因为其 MIME 类型("text/html"(不可执行,并且启用了严格的 MIME 类型检查。

.HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My React App</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body style="margin:0">
<div id="app"></div>
<script type="application/javascript" src="./components/homepage/texting.js"></script>
</body>

webpack 配置:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, '/dist'),
filename: 'index_bundle.js'
},
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
},
},
{
test: /.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {}
}
]
},
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
]
};

没关系,我解决了!我刚刚将所有 JavaScript 移动到 ComponentDidMount((

最新更新