【问题标题】:How to Access Parent Component from Child Component in Angular 9如何在 Angular 9 中从子组件访问父组件
【发布时间】:2020-07-10 14:20:07
【问题描述】:

问题

  • 不能用户 viewContainerRef 来自父子通信。

代码在 Angular 8.2 中运行

 this.viewContainerRef[ '_data' ].componentView.component.viewContainerRef[ '_view' ].component;

错误

ERROR TypeError: Cannot read property 'componentView' of undefined

公共 API https://angular.io/api/core/ViewContainerRef

    this.viewContainerRef.get(0) still i got null

我需要从子组件访问父组件。

吴版

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.900.7
@angular-devkit/core         9.0.7
@angular-devkit/schematics   9.0.7
@schematics/angular          9.0.7
@schematics/update           0.900.7
rxjs                         6.5.3

无法在 stackblitz 中复制问题

https://stackblitz.com/edit/angular-testangular9-communicating-x

问题

ViewContainerRef 在 Angular 9 中不起作用

工作 ViewContainerRef

在 Angular 9 中无法使用 ViewConatinerRef

欢迎提出任何建议。

【问题讨论】:

  • 主要问题是......为什么不能在 stackblitz 上复制它?也许你的项目中还有一些你没有在 stackblitz 上做过的事情?顺便说一句,为了在 stackblitz 上运行它,您应该在 polyfills.ts 中注释 core-js 行(这里是 fixed version)。
  • 谢谢,但我需要通过 viewconatinerRef 方式获取父组件,因为它在 Angular 8.2 中工作的代码在 Angular 9 中是相同的
  • 请查看 viewconatineRef 的更新快照

标签: javascript angular


【解决方案1】:

您不应该(绝对)完全访问 3rd 方库的私人成员以避免遇到麻烦。幸运的是,如果我做对了你想做的事情,你可以做一些事情来避免访问这些私人成员。我将展示 3 个解决方案,其中 2 个在此答案末尾引用的 stackblitz 上实现。

假设您有这种情况:一个父组件有一个 propertyA,您出于某种原因想在子组件中直接访问它。

父组件:

@Component({
  template: `
    <child></child>
  `
})
export class ParentComponent { propertyA: string; }

案例A:将父级直接注入子级

@Component({selector: 'child', ...})
export class ChildComp {
  constructor(private _parent: ParentComponent) { this._parent.propertyA = 'some value'; }
}

案例B:通过孩子的ViewContainerRef获取家长

@Component({selector: 'child', ...})
export class ChildComp {
  constructor(private viewContainerRef: ViewContainerRef) {
    const _injector = this.viewContainerRef.parentInjector;
    const _parent: ParentComponent = _injector.get<ParentComponent>(ParentComponent);  
    this._parent.propertyA = 'some value';
  }
}

这是重要的部分:角度注入器的存在正是为了做你想做的事,所以不要试图自己获取对组件的引用,正确的做法是找出如何找到注入器来进行搜索为你。请记住,注入器是按层次结构分布的,如果注入器无法解析符号,它会要求其父注入器尝试递归解析它,直到到达根注入器。这导致我们采用第三种方法,直接获取组件的注入器:

案例C:通过孩子的注射器获取父母,直接注射

@Component({selector: 'child', ...})
export class ChildComp {
  constructor(private _injector: Injector) { 
    const _parent: ParentComponent = this._injector.get<ParentComponent>(ParentComponent);  
    this._parent.propertyA = 'some value';
  }
}

案例 B 和 C 之所以有效,是因为您的组件以父-n-子关系分布在组件树中,并且在某些时候,它们将在声明/导入它们的模块处具有一个公共注入器。

我已修改 your stackblitz 以显示这两种解决方案(案例 A 用于 a 组件,a 案例 B 用于 b 组件),

【讨论】:

  • 非常感谢,去看看
  • 请注意,viewContainerRef.parentInjector 已从 Angular 7 中弃用,没有替代品。
  • 是否有类似的方法可以在不知道其类型的情况下访问指令中的宿主组件?
  • 也许你可以通过使用引用变量来传递它:&lt;host-c #comp [your-directive]="comp"&gt;&lt;/host-c&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
相关资源
最近更新 更多