简体   繁体   English

将Tailwind.css添加到Vue.js项目后,部分类没有效果

[英]Some classes have no effect after adding Tailwind.css to a Vue.js project

I am trying to add Tailwind.css to a Vue.js project.我正在尝试将 Tailwind.css 添加到 Vue.js 项目。 There are a lot of resources on how to do this, most of them following the same path as this video .有很多关于如何执行此操作的资源,其中大部分遵循与此视频相同的路径。 To make sure I was in the same conditions as in the video, I created a Vue app from scratch, using vue-cli with the default presets.为了确保我处于与视频中相同的条件下,我使用带有默认预设的vue-cli从头开始创建了一个 Vue 应用程序。 After this step, I did the following:完成此步骤后,我执行了以下操作:

  • npm install tailwind.css
  • create src/styles/tailwind.css创建src/styles/tailwind.css
  • adding the following to the css file:将以下内容添加到 css 文件中:
@tailwind base; 
@tailwind components;
@tailwind utilities;
  • call npx tailwind init to create a tailwind.config.js file at the root of the project调用npx tailwind init在项目根目录创建一个tailwind.config.js文件
  • create postcss.config.js at the root of the project, and add the following to this file:在项目的根目录下创建postcss.config.js ,并将以下内容添加到该文件中:
module.exports = {
  plugins: [require("tailwindcss"), require("autoprefixer")],
};
  • add a custom color to the tailwind.config.js file:将自定义颜色添加到tailwind.config.js文件:
module.exports = {   
  theme: {
    colors: {
      "awesome-color": "#56b890",
    },
    extend: {},
  },   
  variants: {},   
  plugins: [], 
};
  • adding a simple <p> element to the HelloWorld.vue component generated by vue-clivue-cli生成的HelloWorld.vue组件添加一个简单的<p>元素
  • trying to style it using Tailwind classes尝试使用Tailwind类对其进行样式设置

Finally, here is the problem: I can apply some classes like bg-awesome-color or text-xl and have them render properly, but a lot other classes won't work.最后,问题来了:我可以应用一些类,如bg-awesome-colortext-xl并让它们正确呈现,但很多其他类将无法工作。

For instance, removing those classes and trying instead bg-black , bg-orange-500 , or text-orange-500 has strictly no effect.例如,删除这些类并尝试改用bg-blackbg-orange-500text-orange-500完全没有效果。 Did I do something wrong?我做错什么了吗? Would that be a problem of compatibility between Vue.js and Tailwind.css?这会是 Vue.js 和 Tailwind.css 之间的兼容性问题吗?

I do not know if this is related, but I also noticed that after adding Tailwind.css, the Vue logo that used to be centered in the original vue-cli template was now aligned left in the page.不知道有没有关系,不过我也注意到在添加Tailwind.css之后,原来在vue-cli模板中居中的Vue logo现在在页面中左对齐了。

Thank you very much for any help!非常感谢您的帮助!

If You want to keep original content, then you should put this inside "extend".如果你想保留原始内容,那么你应该把它放在“扩展”里面。

module.exports = { 
  theme: { 
    extend: {
      colors: { 
        "awesome-color": "#56b890", 
      }, 
    }
  }, 
  variants: {}, 
  plugins: [], 
};

Read more at: https://tailwindcss.com/docs/configuration/阅读更多: https://tailwindcss.com/docs/configuration/

I got the answer from a maintainer of Tailwind.css after posting an issue.发布问题后,我从 Tailwind.css 的维护者那里得到了答案。 I actually misplaced the colors object in tailwind.config.js , causing it to override all existing colors with mine, thus actually removing all the existing ones.我实际上在 tailwind.config.js 中放错了colors tailwind.config.js ,导致它用我的覆盖所有现有的 colors,从而实际上删除了所有现有的。 Here is the correct way to add / override a color without removing all the original ones:这是在不删除所有原始颜色的情况下添加/覆盖颜色的正确方法:

module.exports = {
  theme: {
    extend: {
      colors: {
        "awesome-color": "#56b890",
      },
    },
  },
  variants: {},
  plugins: [],
};

The same thing happened to me, and I spent hours trying to understand why my custom styles weren't working, your error may be in the postcss.config.js , make sure when importing tailwind.config.js you are calling correctly, I leave a couple of examples:同样的事情发生在我身上,我花了几个小时试图理解为什么我的自定义 styles 不工作,你的错误可能在postcss.config.js中,确保在导入tailwind.config.js时你调用正确,我举几个例子:

// postcss.confing.js
const tailwindcss = require("tailwindcss");
const autoprefixer = require("autoprefixer");

module.exports = {
  plugins: [
    tailwindcss("./tailwind.config.js"), // name your custom tailwind
    ...
  ],
};
// postcss.confing.js
module.exports = {
  "plugins": [
    require('tailwindcss')('tailwind.config.js'), // name your custom tailwind
    require('autoprefixer')(),
  ]
}

In both cases it solved the problem for me, I hope it will help you.在这两种情况下,它都为我解决了问题,希望对您有所帮助。

You have to install tailwindcss with vue-tailwind .你必须使用vue-tailwind 安装 tailwindcss

Run npm install tailwindcss .运行npm install tailwindcss

For more information, you can go here https://tailwindcss.com/docs/guides/vite想了解更多可以go这里https://tailwindcss.com/docs/guides/vite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM