Angular @ViewChild返回未定义-角度2

Angular @ViewChild返回未定义-角度2,angular,dom,viewchild,Angular,Dom,Viewchild,我正在尝试使用@ViewChild获取我需要的DOM元素。以下组件描述了我的问题: import {Component, ViewChild, ElementRef, OnInit, Input} from "@angular/core"; @Component({ selector: 'some-comp', template: ` <input #myInputOut type="text" class="google-place-input"

我正在尝试使用@ViewChild获取我需要的DOM元素。以下组件描述了我的问题:

import {Component, ViewChild, ElementRef, OnInit, Input} from "@angular/core";


@Component({
  selector: 'some-comp',
  template: `

  <input
    #myInputOut
    type="text"
    class="google-place-input"
    placeholder="Type to search..">

   <span class="row form-group">
     <required-input
       class="col-12 has-danger"
       [label]="'somel:'"
       [controlName]="'somel'"
       [form]="group"
      </required-input>
    </span>

 <div class="row form-group2 required">
    <label class=" col-3 control-label" for="street">label:</label>
    <input #myInputIn class="col-7" id="someid" placeholder="Type to search.." /></div>
`
})
export class someClass implements OnInit{

  @ViewChild('myInputOut') myInputOut: ElementRef;
  @ViewChild('myInputIn') myInputIn: ElementRef;


  private element: HTMLInputElement;
  private element2: HTMLInputElement;

    constructor(  private databaseService : DatabaseService,
               private router : Router){

  }


  ngOnInit(){
    this.element = this.myInputOut.nativeElement;
    this.element2 = this.myInputIn.nativeElement;
  }
}
import{Component,ViewChild,ElementRef,OnInit,Input}来自“@angular/core”;
@组成部分({
选择器:“某个comp”,
模板:`

看起来您正在对输入进行注释,或者至少开始进行注释

 <!--  <div class="row form-group2 required">
    <label class=" col-3 control-label" for="street">label:</label>
    <input #myInputIn class="col-7" id="someid" placeholder="Type to search.." />

您只能在ngAfterViewInit()内访问ViewChild查询。

它在ngOnInit()中不可用,因此无法定义。

代码中有一些错误,请修复它,然后重试

<required-input
   class="col-12 has-danger"
   [label]="'somel:'"
   [controlName]="'somel'"
   [form]="group"

尝试删除注释开头
您没有正确关闭
所需输入。.尝试关闭it@Andriy这就是问题所在,谢谢。请确保您在此处发布的代码格式正确,语法正确。>在调用ngAfterViewInit回调之前设置视图查询。:
<!--<div class="row form-group2 required">
<input #myInputIn type="text" class="col-7" id="someid" placeholder="Type to search.." />