首页 > 解决方案 > 我们可以在Angular10的@Injectable服务中使用@viewChild吗

问题描述

我有以下代码在 Angular 10 升级之前工作,即 Angular 8。

@injectable 中的 @viewChild 在 Angular8 中工作,但在 Angular10 中没有任何原因?

下面的代码是示例(下面是我现有代码的外观):

@Injectable()
export abstract class DetailBaseComponent
  extends BaseComponent {

/**
   * Reference to the child modal component
   */
  @ViewChild(ModalComponent)
  modal: ModalComponent;

findDetail(){
  console.log(this.modal); //undefined
  this.modal.showModal = true;// error
}

但是当我将@viewChild 从@Injectable 移动到组件类时,它工作正常。有什么理由我们不能使用 @viewChild 除了在组件类中吗?

帮助赞赏!

标签: angularangular10viewchild

解决方案


推荐阅读