Tailwind 的 @apply 指令在 Nuxt 上无法使用

6

我正在尝试在我的全新项目中使用Tailwind,每个工具类都可以正常使用,但@apply这一个却无法编译。

以下是错误信息:

Syntax Error: SyntaxError                                                                                                                                                                                                                                       friendly-errors 08:12:30

(5:5) `@apply` cannot be used with `.lg\:mt-0` because `.lg\:mt-0` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that `.lg\:mt-0` exists, make sure that any `@import` statements are being properly processed *before* Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.

  3 | @import 'tailwindcss/components';
  4 | .navbar-item-link {
> 5 |     @apply text-xs mt-1 lg:mt-0 px-3 no-underline text-gray-600 rounded-full border-solid border border-gray-100 hover:border-blue-best-100;
    |     ^
  6 | }
  7 | /* purgecss end ignore */

我的tailwind.css文件:

/* purgecss start ignore */
@import 'tailwindcss/base';
@import 'tailwindcss/components';
.navbar-item-link {
    @apply text-xs mt-1 lg:mt-0 px-3 no-underline text-gray-600 rounded-full border-solid border border-gray-100 hover:border-blue-best-100;
}
/* purgecss end ignore */

@import 'tailwindcss/utilities';

我已经安装了PostCSS CLI,并且正在使用postcss.config.js,如下所示:

module.exports = {
  plugins: [
    require("postcss-import"),
    require("tailwindcss"),
    require("autoprefixer")
  ]
};

但是这些都不起作用。

1个回答

8

你不能在@apply方法中使用伪类前缀。

应该这样做:

.navbar-item-link {
    @apply text-xs mt-1 lg:mt-0 px-3 no-underline text-gray-600 rounded-full border-solid border border-gray-100 hover:border-blue-best-100;
}

您应该使用这样的代码:
// Normal State
.navbar-item-link {
    @apply text-xs mt-1 px-3 no-underline text-gray-600 rounded-full border-solid border border-gray-100;
}

// Hover State
navbar-item-link:hover{
    @apply border-blue-best-100;
}

// Large Screen
@screen lg {
    .navbar-item-link{
        @apply mt-0;
    }
}

以下是有关此技术的更多信息:

https://tailwindcss.com/docs/extracting-components/#使用apply提取CSS组件

https://tailwindcss.com/docs/functions-and-directives/#screen指令


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接