angular - @ViewChild değişkenlerinin ne türde olması gerekir?
Böyle bir bileşene sahipsam ve bu bileşen yerel bir HTML öğesine referans veriyorsa, hangi tip açıklaması kullanılmalıdır?
@Component({
template: `<video #test></video`
})
export class AppComponent {
@ViewChild('test') test: any;
constructor() {
let nativeElement = this.test.nativeElement;
}
}
Cevap #1
ElementRef<> türü, örn. kullanılmalıdır.
@Component({
template: `<video #test></video`
})
export class AppComponent {
@ViewChild('test') test: ElementRef<HTMLVideoElement>;
constructor() {
let nativeElement = this.test.nativeElement;
// nativeElement now has type HTMLVideoElement
}
}
