简体   繁体   English

@ViewChild值在父级中不可用?

[英]@ViewChild value is not available in parent?

Parent component is: 组件是:

export class DictionaryComponent implements OnInit, AfterViewInit {
  @ViewChild(ViewModeComponent)
  primarySampleComponent: ViewModeComponent;

   ngAfterViewInit() {
      console.log('Values on ngAfterViewInit():');
      console.log(this.primarySampleComponent.viewMode);

    }  

}

Children component is ViewModeComponent : 组件是ViewModeComponent

export class ViewModeComponent {

  public viewMode: mode = 'inline';
  public constructor(
  ) {}

  public switchMode(mode: mode) {
    this.viewMode = mode;
  }
}

Why after changing value this.viewMode in children component I don't receive it value in parent ngAfterViewInit() {} ? 为什么更改子组件中的this.viewMode的值后,我在父ngAfterViewInit() {}没有收到该值?

Console.log says nothing. Console.log什么也没说。

Angular version is 8 角版本为8

Use Observables 使用可观察物

in Service 在服

private viewMode = new BehaviorSubject(false); // create observable variable
checkMode = this.viewMode.asObservable();

changeMode(falg) {
    this.viewMode.next(falg);
}

In Child Component : 在子组件中:

this.viewMode // local variable of Component

public switchMode(mode: mode) {
    this.viewMode = mode;
    this.service.changeMode(mode); // setting value to observable variable
}

in Parent Component : 在父组件中:

this.viewMode // local variable of Component
this.service.checkMode.subscribe(response => this.viewMode = response); // get the value

You can use an EventEmitter to emit values from your child components 您可以使用EventEmitter从子组件中发出值

parent.html parent.html

<ChildComponent (viewModeEvent)="handleViewModeChange($event)"></ChildCompoment>

child.component.ts child.component.ts

Import {..., Output, EventEmitter } from "@angular/core";

export class ViewModeComponent {

  @Output() viewModeEvent = new EventEmitter<mode>(); 
  public viewMode: mode = 'inline';

  public constructor(
  ) {}

  public switchMode(mode: mode) {
    this.viewModeEvent.emit(mode)
  }
}

parent.component.ts parent.component.ts

handleViewModeChange(args: mode) {
  // gets called everytime there is a viewMode change emitted from child
}

You can achieve the same using the EventEmitter . 您可以使用EventEmitter实现相同的EventEmitter Just make an EventEmitter in the child component and pass the event to the parent component..! 只需在子组件中创建一个EventEmitter,然后将事件传递给父组件即可。

child.component.ts child.component.ts

export class ChildComponnent implements OnInit {

  name: EventEmitter<string> = new EventEmitter<string>();

  ngOnInit() {
    setInterval(() => this.name.emit(new Date().toString()), 1000);
  }

}

parent.component.ts parent.component.ts

export class AppComponent implements OnInit {

  @ViewChild('appChild', { static: true }) appChild: ChildComponnent;

  ngOnInit() {
    this.appChild.name.subscribe(console.log);
  }

}

parent.component.html parent.component.html

<app-child #appChild></app-child>
{{(appChild.name | async) | json}}

You can see the live example in the stackblitz here ..! 您可以在此处的堆叠闪电战中看到现场示例。

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

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