Tailwindcss @apply 指令在 vue 组件中不起作用

She*_*din 2 inertiajs vuejs2 tailwind-css laravel-8 laravel-jetstream

我创建了一个带有 jetstream 和惯性 vue 堆栈的 laravel 应用程序,因为我的新项目问题是Tailwindcs版本 2 postCss,它不支持@applyvue 组件内部的指令,但在.css文件内部它工作正常我不想要那个,因为那个 css 文件将加载我的每个页面我只想要带有@apply指令的短内联实用程序类,但我不能,我该如何实现。?

在我的 vue 模板中

<template>
 <div class="mt-4">
  <label for="hello">Hello</label>
  <input id="hello" class="input"/>
 </div>
</templete>

<style scoped>
    .input {
        @apply bg-gray-200 border h-10
    }
</style>
Run Code Online (Sandbox Code Playgroud) 像这样在浏览器中输出

在此处输入图片说明

webpack.mix.js
const mix = require('laravel-mix');

mix.js('resources/js/app.js', 'public/js').vue()
    .postCss('resources/css/app.css', 'public/css', [
        require('postcss-import'),
        require('tailwindcss'),
        require('autoprefixer'),
    ])
    .webpackConfig(require('./webpack.config'));

if (mix.inProduction()) {
    mix.version();
}
Run Code Online (Sandbox Code Playgroud) webpack.config.js
const path = require('path');

module.exports = {
    resolve: {
        alias: {
            '@': path.resolve('resources/js'),
        },
    },
};
Run Code Online (Sandbox Code Playgroud)
tailwind version : "^2.0.1",
laravel version : 8.x,
jetstream version : 2.x,
inertiajs version: "^0.8.2"
Run Code Online (Sandbox Code Playgroud)

小智 5

你必须lang="postcss"<style>标签上设置属性,因为 Tailwind 是一个 PostCSS 插件。

所以改变这个

<style scoped>
    .input {
        @apply bg-gray-200 border h-10
    }
</style>
Run Code Online (Sandbox Code Playgroud)

对此

<style lang="postcss" scoped>
    .input {
        @apply bg-gray-200 border h-10
    }
</style>
Run Code Online (Sandbox Code Playgroud)

  • 使用这种方法会导致以下错误:`模块解析失败:意外的令牌(19:0)文件已使用这些加载器进行处理:* ./node_modules/vue-loader/lib/index.js 您可能需要额外的加载器来处理这些加载器的结果。| | &gt; .btn{ | 白颜色; | }` 有什么想法吗? (2认同)