css - 来自 Gridsome 的 Tailwind 插件的 @apply 不起作用
问题描述
我想在我的 Gridsome 项目中使用 Tailwinds 应用功能。但是会抛出错误,即无法找到来自 tailwind 的 css 类。
重现步骤:
1) 创建新的 Gridsome 项目
3)在assets文件夹中创建css文件并在main.js中导入
import "~/assets/style.css"
4)在css文件中创建css类
5) 在带有尾风组件的 css 中使用 @apply
没有任何反应... css 文件已正确导入,因为应用了正常的 css 某些元素(如 rounded)似乎太有效,但其他元素(如 bg-blue-500)则不适用。
解决方案
据我所知,Tailwind 记录的行为与其实际行为似乎存在脱节。
根据有关使用以下方法提取组件的文档@apply
:
..这样的类应该直接添加到@tailwind components 指令之后以避免特殊性问题。
在实践中,这会生成一个style.css
如下所示的主文件:
@tailwind base;
@tailwind components;
// Note: you could also just add a custom CSS file here.
.btn-blue {
@apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
}
.btn-blue:hover {
@apply bg-blue-700;
}
@tailwind utilities;
然而,实际上,这总是会引发构建错误,并且出于完全合乎逻辑的原因。utilities
Tailwind 使用从文件派生的类注入tailwind.config.js
文件。
CSS 是线性编译的,因此在导入并通读bg-blue-700
之前不存在通过 apply 进行引用。utilities
即使文档建议不要这样做,在解决编译错误之后移动组件类: utilities
@tailwind base;
@tailwind components;
@tailwind utilities;
// Note: you could also just add a custom CSS file here.
.btn-blue {
@apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
}
.btn-blue:hover {
@apply bg-blue-700;
}
不理想,但这对我有用。
推荐阅读
- java - 是否必须拥有相同版本的 jdk 和 jre?
- babeljs - babel-plugin-transform-duplicate-keys 有什么用?
- neo4j - 获取具有特定节点属性值的最新节点
- reactjs - 为什么比较当前道具与具有相同值的 nextProps 会返回 false?
- php - 从两个表中选择数据,将其插入到新表中?(SQL/PHP)
- arrays - Angular 类中的数组数组
- swift - 将字符串的 NSObject 转换为 [String]
- amazon-web-services - AWS-Amplify-cli 无法在 Mac 上安装
- php - 在会话中添加号码无效
- c# - 基础视图的 Xamarin 绑定属性