如何在 Angular 8 中重新加载或刷新子组件

Sau*_*put 19 angular angular5 angular6 angular7 angular8

我有两个组件,一个父级和另一个子级。

HTML 部分

<div>
  <div class="row col-md-12">
    <div class="col-md-4">
       <!-- Some HTML Code of Parent component over here -->
    </div>
    <div class="col-md-8">
      <child-component></child-component>
    </div>
 </div>
 <button class="button" (click)="reloadOnlyChild($event)">Reload Child</button>
</div>
Run Code Online (Sandbox Code Playgroud)

现在,单击此按钮后,我希望唯一的孩子重新加载或刷新。

TS部分

reloadOnlyChild(event){
  // I want to reload the child from here.
}
Run Code Online (Sandbox Code Playgroud)

我在互联网上搜索,我得到的是 Vue 或 React,但不是 Angular。

Aru*_*ini 21

更新子组件的最佳方式是:ngOnChanges()

ngOnChanges(): “当指令的任何数据绑定属性更改时调用的生命周期钩子。定义一个 ngOnChanges() 方法来处理更改。” 我们使用这个生命周期钩子来响应对@Input() 变量的更改。

例子:

import { Component, Input, OnChanges } from "@angular/core";

@Component({
  selector: "child-component",
  templateUrl: "./child-component.html"
})
export class MyComponent implements OnChanges {
  @Input() someInput: string;

  constructor() {}

  ngOnChanges() {
  /**********THIS FUNCTION WILL TRIGGER WHEN PARENT COMPONENT UPDATES 'someInput'**************/
  //Write your code here
   console.log(this.someInput);
  }   
 
}
Run Code Online (Sandbox Code Playgroud)

在父组件中使用子组件如下

<child-component [someInput]="inputValue"></child-component>
Run Code Online (Sandbox Code Playgroud)


小智 18

假设您有一个表单Child.Component.ts并且如果您想从中重置它,parent component您可以使用Subject.

父组件.html

<child-component [resetFormSubject]="resetFormSubject.asObservable()"></child-component>
<button (click)="resetChildForm()"></button>
Run Code Online (Sandbox Code Playgroud)

父组件.ts

import { Subject } from "rxjs";
resetFormSubject: Subject<boolean> = new Subject<boolean>();

resetChildForm(){
   this.resetFormSubject.next(true);
}
Run Code Online (Sandbox Code Playgroud)

子组件.ts

import { Subject } from "rxjs";
@Input() resetFormSubject: Subject<boolean> = new Subject<boolean>();

ngOnInit(){
 this.resetFormSubject.subscribe(response => {
    if(response){
     yourForm.reset();
    // Or do whatever operations you need.
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

通过使用主题,您可以在单击按钮时建立从父级到子级的连接。

希望这个回答有帮助!干杯:)


Gao*_*ter 11

您可以添加一个 Input 来更新组件,或者在您可以在代码中调用的子组件中添加一个更新函数。使用@ViewChild 从父级调用子级更新函数。像这样

https://stackblitz.com/edit/angular-updatechild):

孩子:

import { Component } from "@angular/core";

@Component({
   selector: "app-child",
   templateUrl: "./child.component.html",
   styleUrls: ["./child.component.css"] 
})
export class ChildComponent {
   ticks = Date.now().valueOf();

   constructor() {}

   update(): void {
   this.ticks = Date.now().valueOf();
   }
}
Run Code Online (Sandbox Code Playgroud)

家长:

import { Component, OnInit, ViewChild } from "@angular/core";
import { ChildComponent } from "./../child/child.component";

@Component({
 selector: "app-parrent",
 templateUrl: "./parrent.component.html",
 styleUrls: ["./parrent.component.css"]
})
export class ParrentComponent implements OnInit {
  @ViewChild(ChildComponent, { static: false }) childC: ChildComponent;
  showChild: boolean = true;
  constructor() {}

  ngOnInit() {}

  onUpdateChild() {
    this.childC.update();
 }
}
Run Code Online (Sandbox Code Playgroud)


Adr*_*rma 8

我们还可以使用*ngIfsetTimeout从父组件重置子组件,而无需对子组件进行任何更改。

。模板:

.ts:

show:boolean = true

resetChildForm(){
   this.show = false;

   setTimeout(() => {
      this.show = true
    }, 100);
}
Run Code Online (Sandbox Code Playgroud)

当我们无法控制子组件(如 3rd 方库组件)时,这尤其有用。


Ars*_*mad 6

我使用了这种方法,发现它是最简单的一种。根据你的代码;

<!-- ParentComponent.html -->
<div>
  <div class="row col-md-12">
    <!--- Observe; I added something here --->
    <child-component [someValueToGetChanges]="ValueInput"></child-component>
 </div>
</div>
Run Code Online (Sandbox Code Playgroud)

然后在我们ngOnChanges lifehook的. 当指令的任何数据绑定属性发生更改时,将调用该方法。所以在你的中,我们会这样做;angularchildComponentngOnChangeschildComponent

export class ChildComponent implements OnChanges {
  @Input() 
  someValueToGetChanges: string;
  
  // this code is called when "someValueToGetChanges" value is changed by "ParentComponent"
  ngOnChanges() {
      // Code here what you want
      console.log(this.someValueToGetChanges);
  }   
  
  constructor() {}
}
Run Code Online (Sandbox Code Playgroud)

希望它也对你有用!