[英]TailwindCSS not working in Angular (lazy loaded) Child component
I have been struggling trying to make TailwindCSS work with Angular.我一直在努力让 TailwindCSS 与 Angular 一起工作。 I followed some tutorials and looked in the documentation of Tailwind.
我遵循了一些教程并查看了 Tailwind 的文档。 For some reason in certain places in the Angular 9 application it's working and in lazy loaded modules it's not (so it seems)!
由于某种原因,在 Angular 9 应用程序的某些地方它正在工作,而在延迟加载的模块中它不是(看起来如此)!
So first thing I did was following and implementing these tutorials:所以我做的第一件事就是遵循并实施这些教程:
https://dev.to/seankerwin/angular-8-tailwind-css-guide-3m45 https://dev.to/seankerwin/angular-8-tailwind-css-guide-3m45
https://dev.to/beavearony/building-tailwind-css-in-an-angular-cli-project-e04 https://dev.to/beavearony/building-tailwind-css-in-an-angular-cli-project-e04
I know for sure I have these installed correctly because of the following: My sidebar(app-sidebar) is having the correct css and styling.由于以下原因,我确定我已经正确安装了这些:我的侧边栏(app-sidebar)具有正确的 css 和样式。 But the page I'm on loaded through the is not.
但是我通过 加载的页面不是。
I will provide you with my app-routing.module, and the default layout and the dashboard component.我将为您提供我的 app-routing.module、默认布局和仪表板组件。 On this Dashboard the tailwindCSS is not loading.
在此仪表板上,tailwindCSS 未加载。 (Funny thing: if I add an H1 element with no class, no nothing I see this element on the dashboard page.) The other elements which contain some kind of tailwindCSS class don't, Also if I drag-and-drop through the content of my dashboard component to outside this dashboard component I see my elements.
(有趣的是:如果我添加一个没有 class 的 H1 元素,我在仪表板页面上什么也看不到。)其他包含某种 tailwindCSS class 的元素不会,另外,如果我拖放通过我的仪表板组件的内容到此仪表板组件之外我看到了我的元素。 although without any styling.
虽然没有任何造型。 (I do this with Chrome DevTools)
(我用 Chrome DevTools 做这个)
app-routing.module.ts应用程序路由.module.ts
{
path: 'login',
loadChildren: () => import('./modules/login/login.module').then(m => m.LoginModule)
},
{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full',
},
{
path: '',
component: DefaultLayoutComponent,
children:[
{
path: 'dashboard',
loadChildren: () => import('./modules/dashboard/dashboard.module').then(m => m.DashboardModule)
}
]
},
{ path: '**', redirectTo: '' }
];
default-layout.component.html默认布局.component.html
<div class="h-screen flex overflow-hidden bg-gray-100">
<app-sidebar></app-sidebar>
<div id="content">
<router-outlet></router-outlet>
</div>
</div>
dashboard.component.html仪表板.component.html
<h1>Dashboard</h1>
<div class="flex flex-col w-0 flex-1 overflow-hidden">
<div class="md:hidden pl-1 pt-1 sm:pl-3 sm:pt-3">
<button class="-ml-0.5 -mt-0.5 h-12 w-12 inline-flex items-center justify-center rounded-md text-gray-500 hover:text-gray-900 focus:outline-none focus:bg-gray-200 transition ease-in-out duration-150" aria-label="Open sidebar">
<svg class="h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
</div>
<main class="flex-1 relative z-0 overflow-y-auto pt-2 pb-6 focus:outline-none md:py-6" tabindex="0">
<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
<h1 class="text-2xl font-semibold text-gray-900">Dashboard</h1>
</div>
<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
<!-- Replace with your content -->
<div class="py-4">
<div class="border-4 border-dashed border-gray-200 rounded-lg h-96"></div>
</div>
<!-- /End replace -->
</div>
</main>
</div>
The following I have tried but no succes:以下我尝试过但没有成功:
ViewEncapsulation ( How to get Angular's <router-outlet> to work with CSS Grid ) ViewEncapsulation( 如何让 Angular 的 <router-outlet> 与 CSS Grid 一起使用)
Added on each page a link to the CDN with tailwindCSS在每个页面上添加一个指向带有 tailwindCSS 的 CDN 的链接
Does it maby has any thing to do with the lazy loaded module?它可能与延迟加载的模块有关吗?
Or maby that there is a double router-outlet?或者可能有一个双路由器插座? Because this is how my app.component.html looks
因为这就是我的app.component.html 的样子
<router-outlet></router-outlet>
So I have figured it out.所以我想通了。 There were actually two things left I had to do.
实际上还有两件事我必须做。 First was to set the tailwindcss configuration (tailwind.config.js) and attach it to tailwindcss with the help of my webpack.config.js
首先是设置tailwindcss配置(tailwind.config.js)并在我的webpack.config.js的帮助下将其附加到tailwindcss
module.exports = {
module : {
rules: [
{
test : /\.scss$/,
loader : 'postcss-loader',
options: {
ident : 'postcss',
syntax: 'postcss-scss',
plugins: () => [
require('postcss-import'),
require('tailwindcss')('./tailwind.config.js'),
require('autoprefixer')
]
}
}
]
}
};
Second thing I had to do was putting the overall class of the component to the host in my Angular Component.我要做的第二件事是将组件的整体 class 放到我的 Angular 组件中的主机上。
dashboard.component.ts仪表板.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss'],
host: {
class: "flex flex-col w-0 flex-1 overflow-hidden"
}
})
export class DashboardComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
So looking back at my question and looking at the dashboard.component.html file, the highest div is removed and the class of that div is placed in host.所以回顾我的问题并查看dashboard.component.html文件,最高的div被删除,该div的class被放置在主机中。 Like above.
如上。
I have followed the first link in my question!我已经关注了我的问题中的第一个链接!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.