Introduction
Did you know that you can now combine multiple host-context and selectors in angular?
First, you can create multiple component selectors and use them for styling. This can be very useful to give your component a meaningful name that matches the applied styles.
Here I have created component1,component2,component3 using the below comments.
ng generate component component1
ng generate component component2
ng generate component component3
Once we execute all the commands finally the app.module.ts file is updated like the below code.
- import { NgModule } from'@angular/core';
- import { BrowserModule } from'@angular/platform-browser';
- import { AppRoutingModule } from'./app-routing.module';
- import { AppComponent } from'./app.component';
- import { Component1Component } from'./component1/component1.component';
- import { Component2Component } from'./component2/component2.component';
- import { Component3Component } from'./component3/component3.component';
- @NgModule({
- declarations: [
- AppComponent, Component1Component, Component2Component, Component3Component
- ],
- imports: [
- BrowserModule,
- AppRoutingModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- exportclassAppModule {}
Next, we are going to add a multi-component selector into a single selector. Add the below styles in the app.component.scss page.
- demo1 {
- color: red;
- }.demo2 {
- color: green;
- }.demo3 {
- color: blue;
- }: host - context(.theme - light).demo1 {
- background - color: red;
- }: host - context(.theme - light).demo2 {
- background - color: green;
- }: host - context(.theme - light).demo3 {
- background - color: green;
- }
app.component.html copy the following codes.
- <app-component1class="demo1"></app-component1>
- <app-component2class="demo2"></app-component2>
- <app-component2class="demo3"></app-component2>
You can create multiple component selectors and use them for styling. This can be very useful to give your component a meaningful name that matches the applied styles. It was better than applying classes that can easily be forgotten.
Finally, we get a result like this. I hope this article was most helpful for you.