【问题标题】:Is there a way to import scss without the need of style?有没有一种不需要样式就可以导入 scss 的方法?
【发布时间】:2020-06-05 16:58:55
【问题描述】:

我有一个带有 scss 的 Gatsby 应用程序。

目前我有以下内容:

import React from "react";
import styles from "./mosaic.module.scss";
import Tile from '../tile/tile';

const Mosaic = (): JSX.Element => (
    <section className="wrapper style1">
        <div  className="inner">
            <h2 className="major">Smaller Projects</h2>
            <p>Here under follow minor projects, often unfinished; I show them anyway because they helped me widen my experience. :)</p>
            <section className={`${styles["features"]}`}>
                {[0,1,2,3,4,5,6].map(e => <Tile key={e}></Tile>)}
            </section>
        </div>
    </section>
)

export default Mosaic;

如果我不从 className={${styles["features"]}} 这样的模块中导入类,它将无法工作。

但正如你所见,一些 className 仍然适用于 className="inner"

这是因为在 gatsby-browser.js 下我导入了一些像这样的全局 css

import "./src/styles/global.css"

问题是,知道何时应该使用样式或以正常方式调用一个类真的很混乱。

有没有办法对所有事情都使用默认方式?还是相反?

【问题讨论】:

标签: css reactjs sass gatsby


【解决方案1】:

如果您想要常规的全局 CSS,请将您的文件重命名为 mosaic.scss,导入该文件,然后照常应用您的类名。

import "./mosaic.scss";
...
<section className='features'>...</section>

当您使用*.module.scss 扩展时,您实际上是在告诉gatsby-plugin-sass 您希望使用CSS Modules

如果你想使用 CSS 模块,那么你必须这样做:

import styles from "./mosaic.css";
...
<section className={styles.features}>...</section>

import {features} from "./mosaic.css";
...
<section className={features}>...</section>

编辑:在您在 cmets 中澄清之后,我不认为 gatsby-plugin-sassCSS Modules 做你想做的事。寻找另一种处理 CSS 的工具(例如 stylable.io)可能会更好

【讨论】:

  • 但我相信这不会使类作用于组件
  • 实际上是的,我确认这使得 scss 不局限于组件
  • 当然。但是你特地问了Is there a way to just use the default way for everything ?
  • 确实是的,但我认为Is there a way to just use the default way for everything ? (without loosing the current functionality) 的问题暗示了这一点
  • 我现在明白了。我猜您正在寻找类似于 Angular 的作用域样式工作方式的东西?
猜你喜欢
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
相关资源
最近更新 更多