vue+postcss报错: variable '--primary-color' is undefined and used without a fallback

之前vue-cli3引入postcss的配置: https://www.cnblogs.com/XHappyness/p/7676680.html

发现这么一个问题,我再全局global.css中定义变量

:root {
  --primary-color: red;
}

在其他文件中使用div{color: var(--primary-color)}时,会报错variable '--primary-color' is undefined and used without a fallback

解决办法: https://www.cnblogs.com/XHappyness/p/7676680.html中第二步去掉,改为:

2. 在项目根目录下创建postcss.config.js,内容为:

module.exports = {
  "plugins": {
    "postcss-import": {},
    "postcss-cssnext": {}
  }
}

注意:在使用global.css中定义的css变量时,要@import "目录/Global.css"引入(即使main.ts中已经引入过), 不然还是会报上面的错误

原文地址:https://www.cnblogs.com/XHappyness/p/10072200.html