Angular5 class标签 Angular5给组件本身的标签添加样式class的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Angular5 class标签 Angular5给组件本身的标签添加样式class的方法

ngoing   2021-03-28 我要评论
想了解Angular5给组件本身的标签添加样式class的方法的相关内容吗,ngoing在本文为您仔细讲解Angular5 class标签 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Angular5,class标签,Angular5,添加class样式,Angular5,class样式,下面大家一起来学习吧。

在Angular 5给组件本身的标签添加样式有两种方法:

方式一:使用@Component的host属性

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

在host配置里添加属性,等同于标签上绑定属性的用法一样。

设置style:

  1. '[style.color]': "'red'":注意red值双引号里还有一个单引号。
  2. '[style.background-color]':'backgroundColor':这里是引用了组件里的变量backgroudColor。

这种方式的好处是可以在样式上使用组件的变量。

设置class:

@Component({
 selector : 'myComponent',
 host : {
  '[class.myclass]' : 'showMyClass'
 }
})
class MyComponent {
 showMyClass = false;
 constructor() {
 }

 toggleMyClass() {
  this.showMyClass = !this.showMyClass;
 }
}

方式二:在样式里使用:host选择器

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们