我想使用apply在组件上定义一些CSS样式,同时我还想能够进行覆盖,像这样:
<!-- CustomButton.svelte -->
<script>
let className = '';
export { className as class };
export let label = 'Click me!';
</script>
<button class="custom-button {className}">{label}</button>
<style lang="postcss">
.custom-button {
@apply bg-blue-400 font-bold text-white rounded-lg p-4;
}
</style>
我希望你能翻译一下以下内容:
And I want to use it like this:
<script>
import CustomButton from './CustomButton.svelte';
</script>
<div class="w-screen h-screen flex justify-center items-center">
<CustomButton class="bg-red-800" label="This is my button" />
</div>
也就是说,我想要能够覆盖@applied设置。
问题在于,@apply指令的设置不能被这行代码覆盖。
<button class="custom-button {className}">{label}</button>
我了解到,为了做到这一点,我必须告诉Tailwind在组件层生成相应的CSS,也就是在实用程序之前。
如果我在app.post.css
文件中,在@tailwind utilities
行之前或使用@layer components
指令中输入相同的CSS指令,则可以正常工作:
/* app.post.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
/* This works ok! */
@layer components {
.custom-button {
@apply bg-blue-400 font-bold text-white rounded-lg p-4;
}
}
所以,我希望有一种方法可以告诉Tailwind将我在CustomButton.svelte
组件中定义的.custom-button
样式添加到组件层级,这样它就可以被内联类覆盖。
如果我尝试在CustomButton.svelte
文件中这样做
<style lang="postcss">
@layer components {
.custom-button {
@apply bg-blue-400 font-bold text-white rounded-lg p-4;
}
}
</style>
我遇到了以下错误:
9:13:34 PM [vite] Internal server error: /home/sas/devel/apps/glas-it/apps/wingback/end-user-portal/src/routes/CustomButton.svelte?svelte&type=style&lang.css:1:1: `@layer components` is used but no matching `@tailwind components` directive is present.
Plugin: vite:css
这个问题阻止我在任何组件中使用@apply
指令。
有没有办法解决这个问题?
... 预处理:[预处理({postcss:true,plugins:[tailwindcss,autoprefixer]}),],...
- wkille