css - 我如何不让 CSS 文件在 React 中继承?
问题描述
我目前正在尝试设置一个多页 React 项目,但是当我创建一个新页面时,它会应用每个 CSS 页面,而不仅仅是在*.js
文档开头导入的那个。
例如,我有一个globalstyle.css
文件应用到几乎每个页面上使用
import '../css/globalstyle.css'
的每个页面。
但是,我有一页我不想打开它,但无论如何它都会应用css。globalstyle.css
我猜这是因为App.js
一次加载每个页面,但我不知道如何解决这个问题。
class App extends Component {
render() {
return (
<React.Fragment>
<HashRouter name="app" path="/" handler={App} basename={process.env.PUBLIC_URL}>
<SideNav/>
<TopBar/>
<Switch>
<Route path = "/pages/dashboard.js" exact component={Dashboard} />
<Route path = "/pages/home.js" exact component={Home} />
<Route path = "/pages/tips.js" exact component={Tips} />
</Switch>
<Redirect from="/" exact to="/pages/home.js" />
</HashRouter>
</React.Fragment>
);
}
}
我不想要主页上的 globalstyle。
解决方案
我知道解决这个问题的唯一方法是使用 css 模块。引入 CSS 模块来解决这个问题。
如果您使用的是 create-react-app ,那么它是开箱即用的。参考这里 https://create-react-app.dev/docs/adding-a-css-modules-stylesheet/
如果您想在非 create-react-app 应用程序中进行设置,请参阅此处 https://programmingwithmosh.com/react/css-modules-react/
希望这有帮助。
推荐阅读
- keyboard - 有没有办法获得在 MIPS 中按下一个键的时间?
- javascript - 在地图内多次调用并使用 react-native-select-multiple 显示
- php - 在一行或一个变量中使用 = 和 === 是什么意思?
- javascript - 从服务器页面获取 pc 用户名
- python - 替换python中的特殊字符串
- javascript - Material-UI Autocomplete 仅存储对象的单个属性
- php - 无法在 WooCommerce 中更改产品名称和属性
- java - 为这个返回 RxJava Future 的方法编写单元测试用例
- swift - Swift:如何更准确地检测符号是在墙内还是墙外?
- angular - Angular中的反向地理编码