如何解决为什么我的 css 不能应用于 localhost.3000?
我的 INDEX.CSS 无法应用于我的 LOCALHOST.300。我试图安装 CSS 加载器和东西,但它仍然不起作用,有人可以帮助我吗?! 这是我的 Header.js: 从“反应”导入反应 从 'prop-types' 导入 PropTypes; const 标题 = ({title}) => { 返回 (
<h1>{title}</h1>
</header>
)
}
Header.defaultProps ={
title:'Task Tracker '
}
Header.propTypes={
title:PropTypes.string.isrequired,}
// css in js
// const headingStyle = {
// color:'red',// backgroundColor:'blue'
// }
export default Header
这是我的 App.js:
import React from 'react';
import "./index.css"
import Header from "./components/Header";
function App() {
return (
<div className="Container">
<Header />
</div>
)
}
export default App
这是我的 index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as reportWebVitals from './reportWebVitals';
require('./index.css');
ReactDOM.render(<React.StrictMode>
<App />
</React.StrictMode>,document.getElementById('root'));
这是我的 index.css:
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400&display=swap');
* {
Box-sizing: border-Box;
margin: 0;
padding: 0;
}
body {
font-family: 'Poppins',sans-serif;
}
.container {
max-width: 500px;
margin: 30px auto;
overflow: auto;
min-height: 300px;
border: 1px solid steelblue;
padding: 30px;
border-radius: 5px;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.btn {
display: inline-block;
background: #000;
color: #fff;
border: none;
padding: 10px 20px;
margin: 5px;
border-radius: 5px;
cursor: pointer;
text-decoration: none;
font-size: 15px;
font-family: inherit;
}
.btn:focus {
outline: none;
}
.btn:active {
transform: scale(0.98);
}
.btn-block {
display: block;
width: 100%;
}
.task {
background: #f4f4f4;
margin: 5px;
padding: 10px 20px;
cursor: pointer;
}
.task.reminder {
border-left: 5px solid green;
}
.task h3 {
display: flex;
align-items: center;
justify-content: space-between;
}
.add-form {
margin-bottom: 40px;
}
.form-control {
margin: 20px 0;
}
.form-control label {
display: block;
}
.form-control input {
width: 100%;
height: 40px;
margin: 5px;
padding: 3px 7px;
font-size: 17px;
}
.form-control-check {
display: flex;
align-items: center;
justify-content: space-between;
}
.form-control-check label {
flex: 1;
}
.form-control-check input {
flex: 2;
height: 20px;
}
footer {
margin-top: 30px;
text-align: center;
}
我的 4 个文件(index.css、App.js、Header.js 和 index.js)位于我的 src 文件夹内的 components 文件夹中
这是 package.json:
{
"name": "react-task-number","version": "0.1.0","private": true,"dependencies": {
"@testing-library/jest-dom": "^5.13.0","@testing-library/react": "^11.2.7","@testing-library/user-event": "^12.8.3","react": "^17.0.2","react-build-dist": "^0.0.8","react-dom": "^17.0.2","react-scripts": "^1.1.5","web-vitals": "^1.1.2"
},"scripts": {
"build:dist": "react-build-dist","start": "react-scripts start","build": "react-scripts build","test": "react-scripts test","eject": "react-scripts eject"
},"eslintConfig": {
"extends": [
"react-app","react-app/jest"
]
},"browserslist": {
"production": [
">0.2%","not dead","not op_mini all"
],"development": [
"last 1 chrome version","last 1 firefox version","last 1 safari version"
]
},"devDependencies": {
"css-loader": "^5.2.6","style-loader": "^0.19.0"
}
}
这是 webpack.config.js:
const path = require('path');
module.exports = {
entry: './src/app.js',output: {
filename: 'App.js',path: path.resolve(__dirname,'dist')
},module: {
rules: [
{
test: /\.js$/,exclude: /node_modules/,use: {
loader: 'babel-loader',options: {
presets: ['@babel/preset-env']
}
}
},// Relevant bit of config for style loader and css loader:
{
test: /\.css$/,// the order of `use` is important!
use: [{ loader: 'style-loader' },{ loader: 'css-loader' }],},]
},};
这是 webpack.config.prod.js:
const path = require('path');
const merge = require('webpack-merge');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const common = require('./webpack.common.js');
module.exports = merge(common,{
entry: 'apps/index.js',mode: 'production',devtool: 'source-map',output: {
path: path.resolve(__dirname,'dist/'),publicPath: '/dist/',filename: 'bundle.js',libraryTarget: 'commonjs2',plugins: [
new CleanWebpackPlugin(['dist/*.*']),],externals: {
react: 'react','react-dom': 'react-dom',});
解决方法
因为您在 JSX 中将 'Container' 大写,而您的 css 中的类名是小写
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。