javascript - 父组件的实例如何传递给子组件的构造函数?
问题描述
我试图了解表格组件在 PrimeNG 中的工作原理。我对如何将父组件的实例传递给子组件的构造函数感到困惑。
这是 TableBody 组件的源代码,其中包含 Table 组件的实例。PrimeNG 表源
所以我的理解是 TableBody 组件有一个属性作为选择器。它是从父(表)组件传入的列和 bodyTemplate 中。现在我不明白的是父组件(表)的实例在创建时是如何传递给它的。
表格组件模板的片段
<div class="ui-table-wrapper" *ngIf="!scrollable">
<table #table [ngClass]="tableStyleClass" [ngStyle]="tableStyle">
<ng-container *ngTemplateOutlet="colGroupTemplate; context {$implicit: columns}"></ng-container>
<thead class="ui-table-thead">
<ng-container *ngTemplateOutlet="headerTemplate; context: {$implicit: columns}"></ng-container>
</thead>
<tfoot *ngIf="footerTemplate" class="ui-table-tfoot">
<ng-container *ngTemplateOutlet="footerTemplate; context {$implicit: columns}"></ng-container>
</tfoot>
<tbody class="ui-table-tbody" [pTableBody]="columns" [pTableBodyTemplate]="bodyTemplate"></tbody>
</table>
</div>
完整源码:Github 源码
非常感谢任何见解。
解决方案
父组件中的属性 [pTableBody] 和 [pTableBodyTemplate] 在 ts 文件中声明了 @Input。并且“列”绑定到具有相同名称的变量。
阅读一些关于输入和输出的信息https://www.sitepoint.com/angular-2-components-inputs-outputs/
希望我能有所帮助。
推荐阅读
- c# - Xamarin CrossMediaManager 视频仅适用于第一次运行
- jquery - 使用 Jquery 下载所选文件作为预览选项
- javascript - 创建外部 JS 页面以构建具有动态图表数量和类型的谷歌图表
- java - 在java中的同步块内调用@async方法
- javascript - Puppeteer:在选择器中转义方括号
- python - 硒未启用 chrome 69 上的 Flash
- python - 使用 backend.set_session 时 Keras 无法正确加载模型
- stm32 - 带有 dma 的 stm32f4 adc eoc 标志
- html - 具有不同卡片高度的引导布局在某些分辨率上被打破
- oop - 传递给构造函数或创建