【问题标题】:Tailwindcss @apply directive doesn't work inside vue componentTailwindcss @apply 指令在 vue 组件中不起作用
【发布时间】:2021-04-20 16:39:15
【问题描述】:

我为我的新项目创建了一个带有 jetstream 和惯性 vue 堆栈的 laravel 应用程序,问题是 Tailwindcs 版本 2 使用 postCss 并且它不支持 vue 组件中的 @apply 指令,但在 .css 文件中工作正常我不希望这样,因为该 css 文件将加载我的每个页面

在我的 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>
像这样在浏览器中输出

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();
}
webpack.config.js
const path = require('path');

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

【问题讨论】:

标签: vuejs2 laravel-8 tailwind-css jetstream inertiajs


【解决方案1】:

您必须在 &lt;style&gt; 标签上设置 lang="postcss" 属性,因为 Tailwind 是 PostCSS 插件。

所以改变这个

<style scoped>
    .input {
        @apply bg-gray-200 border h-10
    }
</style>

到这里

<style lang="postcss" scoped>
    .input {
        @apply bg-gray-200 border h-10
    }
</style>

【讨论】:

  • 使用这种方法会导致以下错误:Module parse failed: Unexpected token (19:0) File was processed with these loaders: * ./node_modules/vue-loader/lib/index.js You may need an additional loader to handle the result of these loaders. | | &gt; .btn{ | color :white; | } 有什么想法吗?
  • @BartMommens 快速的 Google 搜索结果是 - github.com/tailwindlabs/discuss/issues/452
  • 感谢您的链接。对于这个问题,我遇到了很多不同的解决方案。从 jetstream 1x 升级到 2x 后,似乎没有一个可以正常工作。最终从 vue 组件的范围样式中删除了所有 @ apply 并将所有内容放在单独的样式表中。没有错误了...
【解决方案2】:

可以暂时解决此问题的解决方法:

.

webpack.config.js

const path = require('path')

module.exports = {
  resolve: {
    alias: {
      '@': path.resolve('resources/js')
    }
  },
  module: {
    rules: [
      {
        test: /\.(postcss)$/,
        use: [
          'vue-style-loader',
          { loader: 'css-loader', options: { importLoaders: 1 } },
          'postcss-loader'
        ]
      }
    ]
  }
}

.

postcss.config.js(如果这个文件不存在,就创建它)

module.exports = {
  plugins: [
    require('postcss-import'),
    require('tailwindcss')('./tailwind.config.js'),
    require('autoprefixer')
  ]
}

.

现在你可以通过添加lang="postcss在Vue文件中使用

<style lang="postcss">
.input-form {
  @apply block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50;
}
</style>

.

此答案基于此处的讨论:https://github.com/laravel-mix/laravel-mix/issues/2778#issuecomment-826121931

【讨论】:

  • 对我来说,我需要删除 vue 样式中的 land="postcss" 才能使其正常工作。
猜你喜欢
  • 2022-06-17
  • 2020-03-09
  • 2021-07-18
  • 2022-08-14
  • 2020-01-07
  • 1970-01-01
  • 2021-08-31
  • 2020-04-12
  • 2021-12-21
相关资源
最近更新 更多