您好,欢迎来到五一七教育网。
搜索
您的当前位置:首页往组件标签内添加样式

往组件标签内添加样式

来源:五一七教育网


这次给大家带来往组件标签内添加样式,往组件标签内添加样式的注意事项有哪些,下面就是实战案例,一起来看一下。

方式一:使用@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 {}

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

Vue+Nuxt.js做出服务端渲染

取得Number中最大与最小元素

Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务