简体   繁体   English

在 Angular 中使用 ViewChild 访问子组件失败

[英]Failing to access child component using ViewChild in Angular

I have a dialog box that is displaying a separate child component.我有一个显示单独子组件的对话框。 The child component is below:子组件如下:

@Component({
  selector: 'userEdit',
  templateUrl: './edituser.component.html',
  styleUrls: ['./edituser.component.css']
})
export class EditUserComponent implements OnInit {
  public theName: string;
  public theAddress: string;

  constructor() {
    this.theName = '';
    this.theAddress = '';
  }

  ngOnInit() {
  }

}

The dialog box code and template are below:对话框代码和模板如下:

@Component({
  selector: 'app-userdialog',
  templateUrl: './userdialog.component.html',
  styleUrls: ['./userdialog.component.css']
})
export class UserDialogComponent implements OnInit {
  @ViewChild('userEdit', {static: false})
  userEdit: EditUserComponent;

  constructor(
    public dlgRef: MatDialogRef<UserDialogComponent>,
   @Inject(MAT_DIALOG_DATA) public theData: UsrStuff) { }

  ngOnInit() {
  }

  ngAfterViewInit() {
    console.log('Name: ' + this.userEdit.theName);
  }

  addUser() {
  // TODO: implement adding a user
 }

  closeBox() {
    this.dlgRef.close();
  }
}

and

<div id="attribdlg">
    <h3>Add New User</h3>
    <userEdit theName="" theAddress=""></userEdit>
    <mat-dialog-actions>
        <button mat-raised-button color="primary" (click)="addUser()">Add User</button>
        <button mat-raised-button color="primary" (click)="closeBox()">Done</button>
    </mat-dialog-actions>
</div>

Based on the documentation and examples Ihave seen, this setup should enable me to print to the console the value pf userEdit's theName property in the ngAfterViewInit() function.根据我看到的文档和示例,此设置应该使我能够将 ngAfterViewInit() function 中的值 pf userEdit 的 theName 属性打印到控制台。

Unfortunately, this does not appear to be working.When the console log is called, I get the following failure message:不幸的是,这似乎不起作用。调用控制台日志时,我收到以下失败消息:

null: TypeError: Cannot read property 'theName' of undefined

Obviously, there is some kind of initialization of the child component that is supposed to happen, but I do not see this being done anywhere in the documentation.显然,应该对子组件进行某种初始化,但我在文档中的任何地方都没有看到这样做。 I am missing something.我错过了一些东西。

How can I get this child component and its properties available to my dialog?我怎样才能让这个子组件及其属性可用于我的对话框?

Two options:两种选择:

  1. Set an id to the component you wish to have with ViewChild() :使用ViewChild()为您希望拥有的组件设置一个id

TypeScript: TypeScript:

@ViewChild('userEdit', {static: false})

HTML: HTML:

<userEdit #userEdit theName="" theAddress=""></userEdit>
  1. Select by directive or component : Select 按directivecomponent

TypeScript: TypeScript:

@import { EditUserComponent } from '...';

@ViewChild(EditUserComponent, {static: false})

HTML: HTML:

<userEdit theName="" theAddress=""></userEdit>
  • I highly recommend you to use app perfix for the component's selector !!!我强烈建议您为组件的selector使用app前缀!!!
@Component({
   ...
      selector: 'app-user-edit',
   ...
})

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

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