【问题标题】:Different css stylesheet for routes路由的不同css样式表
【发布时间】:2019-12-10 21:04:46
【问题描述】:

我对 react 和 react-router-dom 还是很陌生。问题是,我试图在我的应用程序中为不同的路线获得不同的布局。但是,不同路由的所有 css 文件只是导入到一个巨大的冲突文件中。

应用看起来像这样:

<Router>
  < >
    <Navigation />
    <UpButton />
    <Switch>
      <Route path="/" exact component={Home} />
      <Route path="/research" component={Research} />
      <Route path="/publications" component={Publications} />
      <Route path="/student" component={Student} />
      <Route path="/about" component={About} />
    </Switch>
  </>
</Router>

我正在渲染的每个组件如下所示:

import React from 'react';
import './home.scss';

// Utility
import Utility from 'components/utility/Utility';

class Home extends React.Component {
  render() {
    return (
      < >
        <Utility />
      </>
    );
  }
}

export default Home;

有没有办法只导入具有特定路由的特定 css 文件?比如 home.css 对应 '/' 路由,about.css 对应 '/about' 路由?

这可能是我的一个简单的误解,但我现在真的找不到任何解决方案。

【问题讨论】:

    标签: css reactjs create-react-app react-router-dom


    【解决方案1】:

    我建议使用以下文件结构和模式:

    - components
      - Main.js
      - Utility
        - index.js
        - styles.scss
    - pages
        - HomePage
          - index.js
          - styles.scss
    

    然后在组件的样式表中指定与组件相关的样式,并在页面的样式表中指定页面相关的样式。并将页面用于路由,而不是组件。 最后,将每个组件包装到特定的类选择器中,并将所属的 sass 文件添加到该选择器中(如.home-page)。 出于这个原因,请使用 sass 而不是 css,因为您可以在彼此之间嵌入样式定义并将顶部的样式定义用作“命名空间”。所以样式不会相互影响。

    // Main.js
      import HomePage from '../pages/HomePage';
    
      <Switch>
        <Route path="/" exact component={HomePage} />
        <Route path="/research" component={ResearchPage} />
    
    
    // HomePage/index.js
    
      import Utility from './components/Utility';
      import './styles.scss';
      ...
      render(){
        <div className="home-page">
          <Utility />
        </div>
      }
    
    // HomePage/styles.scss    
      .home-page {
        ...
      }
    
    // Utility/index.js
    
      import './styles.scss';
      ...
    
      render(){
        return (
          <div className="utility-comp">
          ...
          </div>
      }
    
    
    // Utility/styles.scss
      .utility-comp {
        ...
      }
    

    【讨论】:

    • 感谢您的回复!我做了同样的事情,仍然将所有样式导入一个巨大的部分。我想,当我进行页面导入时,它会将导入的样式与它们一起使用。
    • 我利用使用 scss 的优势改进了我的答案。请看一下。
    • 信不信由你,我正在使用 .scss 并且现在也这样做了。使用 className="page-name" 将页面组件渲染到 div 中并在其中嵌套布局 .page-name { @contents } 现在可以正常工作了,完美。谢谢!不过,我觉得它可以做得更好。感谢您的帮助和您的时间。
    【解决方案2】:

    React 做了一个 SPA,这意味着:单页应用程序.. 所有资源都在应用程序启动时加载,如果其他资源在第二个时刻加载,则这些资源在整个应用程序中都可用。

    我认为为了避免冲突,您需要更好地定位您的 stile,例如,您可以将页面包装在一个以页面名称为类的容器中,例如:

    class Home extends React.Component {
      render() {
        return (
          <div classname="home">
            <Utility />
          </div>
        );
      }
    }
    
    class Contact extends React.Component {
      render() {
        return (
          <div classname="contact">
            /*...*/
          </div>
        );
      }
    }
    
    

    然后使用页面类定位您的 css:

    .home h1{
    color:red;
    }
    
    .contact h1{
    color:blue;
    }
    

    【讨论】:

    • 哦,我明白了。我现在就试试!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 2022-09-23
    • 2015-12-30
    • 2014-04-11
    • 2010-09-23
    • 2011-06-05
    • 2023-02-13
    相关资源
    最近更新 更多