【发布时间】:2016-08-22 20:49:27
【问题描述】:
我想给我的组件的宿主元素一个类,所以直到现在我都使用了host 属性,如下所示:
@Component({
selector: 'left-bar',
host: { 'class': 'left-bar' },
templateUrl: 'app/left-bar/left-bar.component.html',
styleUrls: ['app/left-bar/left-bar.component.css']
})
export class LeftBarComponent implements OnInit {
ngOnInit() {
}
}
尽管我收到了来自 TypeScript 的警告,说这是一种不好的做法。
[tslint] In the "@Component" class decorator of the class "LeftBarComponent" you are using the "host" property, this is considered bad practice. Use "@HostBindings", "@HostListeners" property decorator instead.
如何以更正确的方式将类添加到宿主元素并消除此警告?
谢谢!
更新:基于以下答案:我得到了类,但添加类后样式不会影响父宿主元素。 我的风格很简单:
.left-bar {
position: fixed;
width: 120px;
left: 0;
top: 0;
bottom: 0;
background: #323232; }
【问题讨论】:
标签: css typescript angular