简体   繁体   English

无法以角度从父组件访问子组件

[英]Can't access child from parent component in angular

I'm newbie in angular.我是角度的新手。 So possibly I just missed smth important because of lack of experience.所以可能我只是因为缺乏经验而错过了重要的事情。 Please, help me to access child component from parent.请帮助我从父级访问子组件。

app.module.ts app.module.ts

import { FilePickerComponent } from "src/app/common/file-picker/file-picker.component";
...
@NgModule({
  declarations: [
  FilePickerComponent,
  ...
  ]})
export class AppModule {}

file-picker.component.ts文件选择器.component.ts

...
@Component({
  selector: "app-file-picker",
  templateUrl: "./file-picker.component.html",
  styleUrls: ["./file-picker.component.css"],  
})
...

editor.html编辑器.html

<form>
    ...
    <app-file-picker
        placeholder="Image"        
        required>
    </app-file-picker>
    ...
</form>

editor.ts编辑器.ts

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

import { FilePickerComponent } from "src/app/common/file-picker/file-picker.component";

export class Editor implements OnInit, OnDestroy {

@ViewChild(FilePickerComponent) filePicker!: FilePickerComponent;
async save() {

}

at the moment when I try to access file picker from save it is undefined.目前,当我尝试从保存中访问文件选择器时,它是未定义的。 how can I access file picker component from editor.ts?如何从 editor.ts 访问文件选择器组件?

export class Editor implements OnInit, OnDestroy {

@ViewChild(FilePickerComponent) filePicker!: FilePickerComponent;
async save() {

}

In the above code the implementation for the AfterViewInit life cycle hook is missing.In angular documentation it states that this life cycle hook is important.在上面的代码中,缺少 AfterViewInit 生命周期钩子的实现。在角度文档中,它指出这个生命周期钩子很重要。

在此处输入图像描述

Angular documentation for component intreactions组件交互的 Angular 文档

Try changing the above code as following尝试更改上面的代码如下

import {
  Component,
  OnInit,
  ViewChild,
  OnDestroy,
  AfterViewInit
} from "@angular/core";


export class Editor implements OnInit, OnDestroy,AfterViewInit {

@ViewChild(FilePickerComponent) filePicker!: FilePickerComponent;
async save() {

}

Try this尝试这个

<app-file-picker #filePicker>

</app-file-picker>

Then in your parent component reference the #filePicker like:然后在您的父组件中引用#filePicker,如:

@ViewChild("filePicker")

Your code is looking fine except small syntactical correction.除了小的syntactical更正外,您的代码看起来不错。 Remove !删除! mark from the variable filePicker .来自变量filePicker的标记。

Change the following code更改以下代码

@ViewChild(FilePickerComponent) filePicker!: FilePickerComponent;

to

@ViewChild(FilePickerComponent) filePicker: FilePickerComponent;

To access to your child component, you need to pick the right angular lifecycle hook first, you have an undefined value on loading of your component because your child component is not yet fully loaded that's why angular didn't recognize the component.要访问您的子组件,您需要首先选择正确的 Angular 生命周期钩子,您在加载组件时有一个未定义的值,因为您的子组件尚未完全加载,这就是 Angular 无法识别该组件的原因。

First, you need to keep '!'首先,您需要保留“!” with the variable to prevent error then you need to deactivate the save() action while the child component is not yet loaded by using a variable isChildLoaded = false for example.使用变量来防止错误,那么您需要在尚未加载子组件时停用 save() 操作,例如使用变量 isChildLoaded = false。

Then you need to use ngAfterViewInit lifecycle hook and wait for the callback, this callback is executed when the child component is fully loaded.然后你需要使用ngAfterViewInit 生命周期钩子并等待回调,这个回调在子组件完全加载时执行。 And there you set the variable = true to activate the save action.并在那里设置变量 = true 以激活保存操作。

    import {
      Component,
      ViewChild,
      AfterViewInit
    } from "@angular/core";
    
    
    export class Editor implements AfterViewInit {
    
    isChildLoaded  = false;
    @ViewChild(FilePickerComponent) filePicker!: FilePickerComponent;
    
    
      ngAfterViewInit() {         
       this.isChildLoaded = true;         
      }
    
    // in your template activate the button only if the variable isChildLoaded=true
    
    async save() {
    
  //now you have full access to the child component 
  //using filePicker variable.
   
 }

}

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

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