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

网友投稿 421 2023-02-09


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:

'[style.color]': "'red'":注意red值双引号里还有一个单引号。

'[style.background-color]'iACsd:'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 {}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:mac连接共享文件夹错误(mac 网络共享文件夹)
下一篇:Spring+Hibernate+Struts(SSH)框架整合实战
相关文章

 发表评论

暂时没有评论,来抢沙发吧~