Angular: nativeElement is undefined on ViewChild

前端 未结 1 1318
遇见更好的自我
遇见更好的自我 2021-02-01 01:53

I want to access the DOM of a component using ViewChild. But when I try to access the nativeElement property, it\'s undefined.

B

相关标签:
1条回答
  • 2021-02-01 02:14

    If you want to get a reference of an element that hosts a component or directive you need to specify that you want the element instead of the component or directive

    @ViewChild('alert', { read: ElementRef }) alert:ElementRef;
    

    See also angular 2 / typescript : get hold of an element in the template

    In your case I guess you need two different @ViewChild() one for the component reference to be able to access the show() method, and a 2nd one to be able to access DOM attributes.

    Plunker example

    0 讨论(0)
提交回复
热议问题