简体   繁体   English

许多 Tailwind CSS 类不适用于我的 Angular 12 项目

[英]Many Tailwind CSS classes doesn´t work on my Angular 12 project

I am developing an Angular 12 project with Tailwind CSS installed.我正在开发一个安装了 Tailwind CSS 的 Angular 12 项目。 I have followed the official docs and it seems everything works;我遵循了官方文档,似乎一切正常; but I can´t understand why some classes work and others not.但我不明白为什么有些课程有效而其他课程无效。

For example, I can have this piece of code, trying to add two Tailwind classes on my div:例如,我可以有这段代码,尝试在我的 div 上添加两个 Tailwind 类:

<div class="text-center mt-2">

    <h2>Please go back to <a class="custom-links" href="./login">login</a></h2>
</div>

And the text-center class works, but the mt-2 doesn´t.文本中心 class 有效,但 mt-2 无效。 This kind of things is happening on the whole project.这种事情正在整个项目中发生。 The way I had to solve it is using traditional CSS or mixing it with Tailwind, like this:我必须解决它的方法是使用传统的 CSS 或将其与 Tailwind 混合,如下所示:

<div id="back-to-login" class="text-center">

    <h2>Please go back to <a class="custom-links" href="./login">login</a></h2>
</div>

And on the css:在 css 上:

#back-to-login{
    
    margin-top: 40px;

}

Then it works fine and the margin-top is applied.然后它工作正常并应用了margin-top。

Do you know what could be happening?你知道会发生什么吗?

Reinstalling node_modules like suggested here doesn´t solve it.这里建议的那样重新安装 node_modules 并不能解决它。

Thanks a lot.非常感谢。

I add the code of the styles.css and tailwind.config我添加了 styles.css 和 tailwind.config 的代码

styles.css styles.css

/* You can add global styles to this file, and also import other style files */
@tailwind base;
@tailwind components;
@tailwind utilities;

@font-face {
  font-family: "firechat";
  src: url(./assets/fonts/blazed/Blazed.ttf);
}

/*
  to change the default h1 styles on tailwind

  https://tailwindcss.com/docs/preflight#extending-preflight

*/
@layer base {
  h1 {
    @apply text-6xl;
  }
}

/*tailwind and own styles*/

#firechat-font{
  font-family: "firechat";
  color:red;
}

.custom-links{
  color:red;
  font-weight: bold;
}

Tailwind config file:顺风配置文件:

module.exports = {
  content: [
    "./src/**/*.{html,ts}"
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

EDIT: What I am seeing now is that for example mt-2 applies and appear on devTools (maybe problem was it was to small change to notice, my fault), but a bigger margin like mt-4 or mt-6 doesn´t.编辑:我现在看到的是,例如 mt-2 应用并出现在 devTools 上(也许问题是需要注意的微小变化,我的错),但是像 mt-4 或 mt-6 这样的较大边距不会. It happened also with other properties.它也发生在其他属性上。

It seems that you've installed the latest version of tailwind with angular 12 maybe something is wrong with it?看来您已经安装了最新版本的顺风 angular 12 可能有问题吗? Try install tailwind v2 and see if it works.尝试安装tailwind v2,看看它是否有效。 Some breaking changes could happen since tailwind v3自tailwind v3以来可能会发生一些重大变化

Check out this tutorial: https://medium.com/tunaiku-tech/angular-11-tailwindcss-2-0-blazing-fast-cfa20ae3a5e9 .查看本教程: https://medium.com/tunaiku-tech/angular-11-tailwindcss-2-0-blazing-fast-cfa20ae3a5e9 In that tutorial, TailwindCSS version 2 is used.在该教程中,使用了 TailwindCSS 版本 2。 If you want TailwindCSS version 3, then kindly change the configuration file to match the TailwindCSS version 3 config file.如果您想要 TailwindCSS 版本 3,请更改配置文件以匹配 TailwindCSS 版本 3 配置文件。 By setting up the angular project as mentioned in the above tutorial TailwindCSS version 3 is working fine.通过设置 angular 项目,如上述教程中所述,TailwindCSS 版本 3 工作正常。

For some reason, in my styles.scss , I had to import the variables as follows出于某种原因,在我的styles.scss中,我必须按如下方式导入变量

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

instead of代替

@tailwind base;
@tailwind components;
@tailwind utilities;

reload and it worked.重新加载,它工作。 Angular version 14.1.0, Tailwindcss version 3.1.7 Angular 版本 14.1.0,Tailwindcss 版本 3.1.7

Thanks to @MaksatRahmanov I found the solution.感谢@MaksatRahmanov,我找到了解决方案。 It seems the problem was I installed the latest Tailwind version (v3) with Angular 12. I switched back to v2 and everything works fine.看来问题是我用 Angular 12 安装了最新的 Tailwind 版本(v3)。我切换回 v2,一切正常。

The only problem with it is that many things have changed between both versions ( check here ), so it could break many things working properly with v3.唯一的问题是两个版本之间发生了很多变化(检查这里),所以它可能会破坏许多在 v3 中正常工作的东西。

This also happened to me.这也发生在我身上。 I checked my tailwind.config.js many times, and我检查了我的tailwind.config.js 很多次,并且

content: [    
"../src/**/*.html"
]

...should be enough.But for some reason unknown to me, it just doesn't rebuild on adding new classes. ...应该足够了。但由于某种我不知道的原因,它只是不会在添加新类时重建。 So, same as you, I didn't see new classes in built CSS files.所以,和你一样,我没有在内置的 CSS 文件中看到新类。 I almost gave up, but in the end here is my workaround:我几乎放弃了,但最后这是我的解决方法:

src/tailwind.scss:源/tailwind.scss:

@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";

New script in package.json, called "tailwind" - it watches for changes in your HTML and rebuilds your css. package.json 中的新脚本,称为“顺风” - 它监视您的 HTML 的变化并重建您的 ZC7A628CBA2AE2E2EB666755Z.

postcss.config.js: postcss.config.js:

module.exports = {
plugins: {
    tailwindcss: {},
    autoprefixer: {},
    }
}

Also installed npm-run-all, so I can on "npm run start" serve and watch tailwind for changes:还安装了 npm-run-all,所以我可以在“npm run start”上服务并观察顺风的变化:

"scripts": {
"ng": "nx",
"serve": "ng serve --configuration=dev",
"start": "npm-run-all --parallel serve tailwind",    
"tailwind": "npx tailwindcss --postcss -i ./src/tailwind.scss -o ./src/app/scss/tailwind.css --watch"
 }

angular.json: angular.json:

"styles": [
            "src/app/scss/tailwind.css"
          ]

And there you go.还有你 go。 You will serve and rebuild CSS whenever new classes are added to your HTML files.每当将新类添加到 HTML 文件时,您将提供并重建 CSS。 I know it's a workaround, but at least it works 100%.我知道这是一种解决方法,但至少它可以 100% 工作。

Angular 14 Try tailwind.config.js like this Angular 14 像这样尝试tailwind.config.js

/** @type {import('tailwindcss').Config} */
  module.exports = {
  content: ['./src/**/*.{html,ts}', './projects/**/*.{html,ts}'],
  theme: {
    extend: {},
  },
  plugins: [],
 }

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

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