这次给大家带来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 {}
登录后复制
相信看了本文案例你已经掌握了方法,更多精彩请关注【创想鸟】其它相关文章!
推荐阅读:
React Router v4使用详解
vue表单入门使用须知
以上就是Angular5对组件标签添加样式class步骤说明的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2765874.html