如何在带有css模块的react组件中使用@apply



我已经在全局style。css:

:root {
    --font: {
        font-family: "Source Sans Pro", sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
}

…在react组件的style.css中:

.rightnav {
    @apply --font;
    float: right;
    color: white;
}

在组件本身中:

import React from "react"
import cssModules from "react-css-modules"
import style from "./style.css"
render() {
  return (
        <ul>
          <li className={style.rightnav}>Blog</li>
        </ul>
  )
}

我得到一个错误:没有自定义属性集声明的font .

必须将全局样式表导入到本地样式表中。因为postcss-apply在解析本地文件时无法了解全局文件。因此,让一个局部只包含自定义属性和自定义属性集声明是个好主意。

你可能需要postcss-import

@import 'path/to/variables.css';
.rightnav {
  @apply --font;
  float: right;
  color: white;
}

最新更新