• react-demo项目:支持使用CSS Modules(不使用create-react-app脚手架)


    node版本为v14.17.0
    项目使用css modules必须先进行配置

    webpack.config.js文件配置

    
      module: {
        rules: [{
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            use: {
              loader: 'babel-loader',
            },
          },
          {
            test: /\.html$/,
            use: [{
              loader: 'html-loader',
            }, ],
          },
          {
            test: /\.css$/,
            use: [{
              loader: 'style-loader',
            }, {
              loader: 'css-loader',
              options: {
                modules: true//开启modules
              }
            }],
          },
        ],
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    更多详细查看:https://webpack.docschina.org/loaders/css-loader/

    //App.jsx
    import React, { useEffect } from 'react';
    import { Outlet, useNavigate, useLocation } from 'react-router-dom';
    import style from './App.css';
    
    function App() {
      const navigate = useNavigate();
      // 获取当前路由的信息
      const location = useLocation();
    
      const go = (link) => {
        navigate(link, { state: {} });
    
      };
    
    
      useEffect(() => {
        console.log('location', location);
        // console.log('navigate',navigate);
      }, [location])
      return (
        <div className={style['H-layout']}>
          <div className={style['H-header']}>
            <span> hello Work</span>
          </div>
    
          <div className={style['H-body']}>
            <div className={style["H-silder"]}>
              <button type="button" className={style["btn"]} onClick={() => go('/home')}>
                Home
              </button>
              <button type="button" className={style["btn"]} onClick={() => go('/looking')}>
                Looking
              </button>
              <button type="button" className={style["btn"]} onClick={() => go('/about')}>
                about
              </button>
            </div>
            <div className={style["H-container"]}>
              <Outlet />
            </div>
          </div>
        </div>
      );
    }
    
    export default App;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    App.css文件

    
    /* 按钮样式 */
    .btn {
      height: 28px;
    }
    
    /* 布局样式 */
    .H-layout {
    }
    .H-header {
      height: 100px;
      background-color: aqua;
    }
    .H-body {
      display: flex;
    }
    
    .H-silder {
      width: 200px;
      background-color: bisque;
      display: flex;
      flex-direction: column;
      padding: 10px 20px;
    }
    .H-silder .btn {
      margin-bottom: 10px;
    }
    .H-container {
      width: calc(100% - 200px);
      background-color: aquamarine;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    如何应对网络攻击?
    jadx 反编译apk
    IIS发布.net网站(配置Nginx以及HTTP和HTTPS)
    Linux 的交换空间(swap)是什么?有什么用?
    一套 .NET开发的邮箱Mail开源库
    【图像识别】基于神经网络实现肺癌图像识别研究附matlab代码
    Mybatis做批量操作
    Linux面试题
    二维平面扭曲的python实现及思路
    java - 包装类
  • 原文地址:https://blog.csdn.net/weixin_40119412/article/details/128035656