Vas*_*its 10 css typescript angular
我想给我的组件的host元素一个类,所以直到现在我使用这样的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() {
}
}
Run Code Online (Sandbox Code Playgroud)
现在虽然我收到了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.
Run Code Online (Sandbox Code Playgroud)
如何以更正确的方式向主元素添加类并摆脱此警告?
谢谢!
更新:基于以下答案:我得到了类,但是在添加类之后样式不会影响父主机元素.我的风格很简单:
.left-bar {
position: fixed;
width: 120px;
left: 0;
top: 0;
bottom: 0;
background: #323232; }
Run Code Online (Sandbox Code Playgroud)
Gün*_*uer 17
Angular2风格指南说更喜欢@HostBinding,但这并不是host: {...}一件坏事.
您可以使用
@Component({
selector: 'left-bar',
templateUrl: 'app/left-bar/left-bar.component.html',
styleUrls: ['app/left-bar/left-bar.component.css']
})
export class LeftBarComponent implements OnInit {
@HostBinding('class.left-bar') leftBarClass = true;
// or @HostBinding('class') leftBarClass = 'left-bar';
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13359 次 |
| 最近记录: |