【问题标题】:SCSS modules not loading in NextJSSCSS 模块未在 NextJS 中加载
【发布时间】:2021-01-16 10:49:31
【问题描述】:

我不确定问题出在哪里,因为我已经多次这样做了,没有任何问题,所以我认为我搞砸了。我将展示 Product.js 组件和模块文件,如果我还缺少任何其他内容,请告诉我(例如,也没有 next.config.js 文件)。

提前致谢!

产品.js

import { Card } from '@material-ui/core'
import styles from '../styles/Products.module.scss'

export default function Product({ product }) {
  console.log(product)
  return(
    <Card className="product">
      <img src={product.main_image} />
      {product.name && product.name}
    </Card>
  )
}

Products.module.scss 文件

.product {
  max-width: 300px;

  & img {
    width: 100%;
  }
}

【问题讨论】:

    标签: reactjs sass next.js css-modules


    【解决方案1】:

    使用 CSS 模块时,您应该将您的 styles 对象传递给 className,并访问所需的类:

    import { Card } from '@material-ui/core'
    import styles from '../styles/Products.module.scss'
    
    export default function Product({ product }) {
      console.log(product)
      return(
        <Card className={styles.product}>
          <img src={product.main_image} />
          {product.name && product.name}
        </Card>
      )
    }
    

    这是因为 CSS 模块会生成一个唯一的类名。它的类名不会是product,而是类似[filename]\_[classname]\_\_[hash](带有唯一哈希)以避免名称冲突。

    adding CSS module

    【讨论】:

    • 天哪。我怎么错过了!?天哪,我的大脑真的那么晚了吗?谢谢一百万!
    • 不客气 :) 这发生在我们所有人身上,呵呵。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-05-05
    • 2022-12-31
    • 1970-01-01
    • 2021-10-06
    • 2022-07-03
    • 2017-05-24
    • 2019-05-02
    相关资源
    最近更新 更多