react实现局部样式、全局样式、antd-mobal按需共存

前言

众所周知,react的css在默认情况下无论在哪个组件引用,都是全局的,后 覆盖 前(对于权重高的不覆盖,相当于css的书写顺序)

准备

react暴露配置文件

react配置less-loader

配置antd-mobal按需引入

在上面基础之上,解决以下问题

1、实现全局css以及局部css

2、局部css适配antd-mobal的按需引入

webpack.config.js
把之前配置less产生的部分代码恢复为

const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;

{
  test: cssRegex,
  exclude: cssModuleRegex,
  use: getStyleLoaders({
    importLoaders: 1,
    sourceMap: isEnvProduction && shouldUseSourceMap,
  }),
  // Don't consider CSS imports dead code even if the
  // containing package claims to have no side effects.
  // Remove this when webpack adds a warning or an error for this.
  // See https://github.com/webpack/webpack/issues/6571
  sideEffects: true,
},
{
  test: cssModuleRegex,
  use: getStyleLoaders({
    importLoaders: 1,
    sourceMap: isEnvProduction && shouldUseSourceMap,
    modules: {
      getLocalIdent: getCSSModuleLocalIdent,
    },
  }),
},

添加以下代码

const lessRegex = /\.less$/;
const lessModuleRegex = /\.module\.less$/;
{
  test: lessRegex,
  exclude: lessModuleRegex,
  use: getStyleLoaders(
    {
      importLoaders: 3,
      sourceMap: isEnvProduction && shouldUseSourceMap,
    },
    'less-loader'
  ),
  sideEffects: true,
},
// Adds support for CSS Modules, but using LESS
// using the extension .module.scss or .module.less
{
  test: lessModuleRegex,
  use: getStyleLoaders(
    {
      importLoaders: 3,
      sourceMap: isEnvProduction && shouldUseSourceMap,
      modules: {
        getLocalIdent: getCSSModuleLocalIdent,
      },
    },
    'less-loader'
  ),
},

以下为关键代码选区

使用

使用全局css

定义全局.less文件,
index.js中

import './styles/global.less';


使用局部css

AntdTest.jsx

import React, {Component} from 'react'
import {withRouter} from "react-router-dom";
import style from './AntdTest.module.less'
class AntdTest extends Component {
  render() {
    return (
      <div>
        <div className={style.box}></div>
      </div>
    )
  }
}
export default withRouter(AntdTest);
.box{
  background: #0e80d2;
  width: 100px;
  height: 100px;
}

AntdTest.module.less

.box{
  background: #0e80d2;
  width: 100px;
  height: 100px;
}


注意:命名局部css的时候,一定要加上modal.less,如xxx.modal.less,否则不匹配生效

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用antd-mobile和react-router-dom来实现tabs功能。首先,你要安装antd-mobile和react-router-dom依赖包。 ```shell npm install antd-mobile react-router-dom ``` 接下来,你可以创建一个Tabs组件,并在其中使用antd-mobile的TabBar组件来实现tabs布局。然后,使用react-router-dom的Route组件来定义每个tab对应的页面。 ```jsx import React from 'react'; import { TabBar } from 'antd-mobile'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; const Tabs = () => { const tabItems = [ { title: 'Tab 1', path: '/tab1', component: Tab1 }, { title: 'Tab 2', path: '/tab2', component: Tab2 }, { title: 'Tab 3', path: '/tab3', component: Tab3 }, ]; const renderTabs = () => { return tabItems.map((item) => ( <TabBar.Item key={item.path} title={item.title} icon={<div />} selectedIcon={<div />} selected={window.location.pathname === item.path} onPress={() => { window.location.href = item.path; }} /> )); }; return ( <Router> <div style={{ position: 'fixed', width: '100%', bottom: 0 }}> <TabBar>{renderTabs()}</TabBar> </div> {/* Define routes */} {tabItems.map((item) => ( <Route key={item.path} path={item.path} component={item.component} /> ))} </Router> ); }; const Tab1 = () => <div>Tab 1 content</div>; const Tab2 = () => <div>Tab 2 content</div>; const Tab3 = () => <div>Tab 3 content</div>; export default Tabs; ``` 在上面的代码中,我们定义了三个tab,分别对应不同的路径和组件。通过点击tab,我们可以切换显示不同的内容。你可以根据自己的求修改tab的数量、标题、路径和对应的组件。 希望这可以帮助你实现antd-mobile和react-router-dom的tabs功能!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值