gpt4 book ai didi

javascript - style-loader 不会在 index.html 的 HEAD 部分注入(inject) CSS 代码

转载 作者:行者123 更新时间:2023-12-02 20:54:40 25 4
gpt4 key购买 nike

我是一个 Webpack 新手,我正在遵循一个非常基本的教程。我不明白为什么 style-loader 没有按照教程中的描述在我的 index.html 的 HEAD 标签中注入(inject) CSS 代码。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>It works!</h1>
</body>
<script src="dist/index.js" />
</html>

样式.css

body {
background-color: red;
}

index.js

import { Template } from './template';
import './style.css';

let customers = [
{name: "Mario", surname: "Rossi"},
{name: "Anna", surname: "Verdi"},
{name: "Carlo", surname: "Bianchi"}
];

let template = new Template('customers');
template.download();
template.render(customers);

模板.js

export class Template {
constructor(name) {
this.name = name;
}

download() {
console.log(`Template ${this.name} downloaded`);
}

render(data) {
console.log(data);
}
}

webpack.config.js

const path = require("path");

module.exports = {
entry: './src/index.js',
//mode: 'development',
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
}
}

应用程序结构

+ dist
--- index.js
+ src
--- index.js
--- template.js
--- style.css
index.html
webpack.config.js

我错过了什么?感谢您的帮助

最佳答案

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>It works!</h1>
<script src="./index.js"></script>
</body>
<!-- <script src="dist/index.js" /> DELETED -->
</html>

应用程序结构

+ dist
--- index.js
--- index.html (Moved here)
+ src
--- index.js
--- template.js
--- style.css
webpack.config.js

关于javascript - style-loader 不会在 index.html 的 HEAD 部分注入(inject) CSS 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61516920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com