2016-04-28 14 views
8

@hostbinding ile açısal 2'de bir konak elemana sınıfını vermek için: gerçi Şimdişu ana kadar böyle <code>host</code> özelliği kullanılan bu yüzden benim bileşenin konak elemanı için bir sınıf vermek istiyorum

@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'ten bunun kötü bir uygulama olduğuna dair bir uyarı alıyorum.

[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. 

Nasıl daha doğru bir şekilde ev sahibi öğesine bir sınıf ekleyebilir ve bu uyarı kurtulmak?

Thx!

Güncelleme: Aşağıdaki cevaba göre: Sınıfı aldım ancak stil, sınıf eklendikten sonra ana ana makine öğesini etkilemiyor. Tarzım oldukça basittir:

.left-bar { 
    position: fixed; 
    width: 120px; 
    left: 0; 
    top: 0; 
    bottom: 0; 
    background: #323232; } 

cevap

14

Angular2 stil kılavuzu @HostBinding tercih söylüyor ama bu host: {...} kötü bir şey yapmaz.

Derslere ekler ancak ben ne zaman bu benim tarzım konak öğesi etkilemiyor

@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() { 
    } 

} 
+0

Thx kullanabilirsiniz. –

+0

Sınıf gerçekten eklenmişse tarayıcı devtools'u kontrol ettiniz mi? Belki stilleriniz doğru değildir. –

+0

Evet, sınıf eklendi ve stilin doğru olduğunu doğruladım. Tüm çocuk elemanları için çalışır, ancak ana eleman için değil. –

İlgili konular