angular 2 从@ViewChild 组件重新渲染父组件

angular 2 re-render parent component from @ViewChild Component

我正在开发 angular2 应用程序,我的父组件会弹出一个子组件

@ViewChild(childPopupComponent) case: childPopupComponent;

我想做的是:当从子组件(在父组件内弹出)按下保存按钮时,重新渲染父组件以进行反射(而不是重新加载整个页面)。

我知道这会重新渲染当前组件,但是如何从 @ViewChild一个

this.ref.detectChanges();

在您的子组件中注入 ChangeDetectorRef,例如:

export class childPopupComponent {
    constructor(public cdRef: ChangeDetectorRef) {}

之后您将能够 运行 仅在子组件上循环更改检测:

@ViewChild(childPopupComponent) case: childPopupComponent;

this.case.cdRef.detectChanges();

要更新父级,您可以将其注入子级

export class childPopupComponent {
    constructor(public parent: ParentComponent) {}

然后

this.parent.cdRef.detectChanges()

甚至试试这个:

import { ChangeDetectorRef, SkipSelf } from '@angular/core';

export class childPopupComponent {
    constructor(@SkipSelf() private parentCdRef: ChangeDetectorRef) {}