简体   繁体   English

在Angular(Angular 4)中,无法使用@ViewChild访问另一个组件的ElementRef

[英]In Angular (Angular 4) ,Not able to access the ElementRef of another Component using @ViewChild

I was trying to access the child component using @ViewChild in Parent Component(AppComponent.ts) 我试图使用父组件(AppComponent.ts)中的@ViewChild访问子组件

      import { Component, ViewChild, ElementRef, AfterViewInit} from '@angular/core';
      import { HeaderComponent } from '../app/components/header/header.component';
      @Component({
        selector: 'app-root',
        templateUrl: './app.component.html',
        styleUrls: ['./app.component.less']
      })
      export class AppComponent implements AfterViewInit {
        title = 'app';
        @ViewChild('myHeader') myHeader: ElementRef;
        ngAfterViewInit() {
          this.myHeader.nativeElement.style.backgroundColor = 'red';
        }  
      }

Parent HTML(app.component.html) 父HTML(app.component.html)

<div>
    <app-header></app-header>
</div>

Now Child HeaderComponent is as below: 现在,Child HeaderComponent如下:

    import { Component, OnInit, ViewEncapsulation } from '@angular/core';

    @Component({
      selector: 'app-header',
      templateUrl: './header.component.html',
      styleUrls: ['./header.component.less']
    })
    export class HeaderComponent implements OnInit {
      constructor() {
      }
      ngOnInit() {

      }

    }

Header HTML(header.component.html)(Child) 标头HTML(header.component.html)(子级)

<div #myHeader> hello</div>

In ngAfterViewInit function this.myHeader.nativeElement is giving error. 在ngAfterViewInit函数中,this.myHeader.nativeElement正在给出错误。

AppComponent_Host.html:1 ERROR TypeError: Cannot read property 'nativeElement' of undefined
at AppComponent.webpackJsonp.../../../../../src/app/app.component.ts.AppComponent.ngAfterViewInit (app.component.ts:12)
at callProviderLifecycles (core.es5.js:11189)
at callElementProvidersLifecycles (core.es5.js:11164)
at callLifecycleHooksChildrenFirst (core.es5.js:11148)
at checkAndUpdateView ....

Although when i tried to access any child defined in Parent HTML(app.component.html) its working like below: 虽然当我尝试访问父HTML(app.component.html)中定义的任何子级时,其工作方式如下:

<div #myHeader> <app-header></app-header></div>

But i want to access any ElementRef in header.component.html.I tired to have View Encapsulation also like below. 但是我想访问header.component.html中的任何ElementRef。我很累也有如下所示的View Encapsulation。

encapsulation: ViewEncapsulation.None,

in HeaderComponent but it also not working.Please let me know what else i am missing here. 在HeaderComponent中,但它也无法正常工作。请让我知道我在这里还缺少什么。

When you are using Component as @ViewChild you can directly set their instance instead of ElementRef Component用作@ViewChild ,可以直接设置其实例而不是ElementRef

@ViewChild(HeaderComponent) myHeader: HeaderComponent;

In case you are using ElementRef you should specify the selector in the HTML as below, 如果您使用的是ElementRef ,则应在HTML中指定选择器,如下所示:

<div #myHeader> hello</div>

You can't access element reference from child components template directly. 您不能直接从子组件模板访问元素引用。 You can access HeaderComponent in your AppComponent, using header components reference you can access all public properties of HeaderComponent. 您可以在AppComponent中访问HeaderComponent,使用标题组件参考可以访问HeaderComponent的所有公共属性。 In HeaderComponent you can access any element from HeaderComponent's template & expose that as public property. 在HeaderComponent中,您可以访问HeaderComponent模板中的任何元素,并将其公开为公共属性。

import { Component, OnInit, ViewEncapsulation, ViewChild, ElementRef} from '@angular/core'; 

@Component({ selector: 'app-header', templateUrl: './header.component.html', 

styleUrls: ['./header.component.less'] 
    }) 
export class HeaderComponent implements OnInit { 
        @ViewChild('myHeader')
     public myHeader: ElementRef; 
        constructor() { } 
        ngOnInit() { } 
        }

  /*---- AppComponent----*/
    import { Component, ViewChild, ElementRef, AfterViewInit} from '@angular/core';
 import { HeaderComponent } from '../app/components/header/header.component';



@Component(
{ selector: 'app-root', 
templateUrl: './app.component.html', styleUrls: ['./app.component.less'] 
}) 
export class AppComponent implements AfterViewInit 
{ title = 'app'; 

@ViewChild(HeaderComponent) 
header: HeaderComponent;

 ngAfterViewInit() { 
this.header.myHeader.nativeElement.style.backgroundColor = 'red'; 
}

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM