这次给大家带来怎样使用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:
'[style.color]': "'red'":注意red值双引号里还有一个单引号。
'[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 {}
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
怎样正确使用vuex项目结构目录与配置
怎样处理vue-router懒加载时候第一次加载资源过多导致速度缓慢
以上就是怎样使用angular5为组件标签添加样式class的详细内容。