Home > Web Front-end > JS Tutorial > Add styles to component tags

Add styles to component tags

php中世界最好的语言
Release: 2018-06-11 10:19:42
Original
1750 people have browsed it

This time I will introduce to you the precautions for adding styles to component tags and adding styles to component tags. The following is a practical case, let's take a look.

Method 1: Use the host attribute of @Component

@Component({
 selector : 'myComponent',
 host : {
  '[style.color]' : "'red'", 
  '[style.background-color]' : 'backgroundColor'
 }
})
class MyComponent {
 backgroundColor: string;
 constructor() {
  this.backgroundColor = 'blue';
 }
}
Copy after login

Add attributes in the host configuration, which is equivalent to the usage of binding attributes on labels.

Set style:

  1. '[style.color]': "'red'": Note that there is a single quote inside the double quotes of the red value.

  2. '[style.background-color]':'backgroundColor': This refers to the variable backgroundColor in the component.

The advantage of this method is that you can use component variables in styles.

Set class:

@Component({
 selector : 'myComponent',
 host : {
  '[class.myclass]' : 'showMyClass'
 }
})
class MyComponent {
 showMyClass = false;
 constructor() {
 }
 toggleMyClass() {
  this.showMyClass = !this.showMyClass;
 }
}
Copy after login

Method 2: Use: host selector in the style

@Component({
 selector : 'myComponent',
 styles : [`
  :host {
   color: red;
   background-color: blue;
  }
 `]
})
class MyComponent {}
Copy after login

I believe you have mastered the method after reading the case in this article , for more exciting content, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Vue Nuxt.js makes server-side rendering

Get the largest and smallest element in Number

The above is the detailed content of Add styles to component tags. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template