【发布时间】:2020-02-24 09:02:55
【问题描述】:
我无法获取另一个父方法,该方法在我用作输入并在子组件中调用的父方法中调用。
export class TreeMapComponent {
data;
constructor(public dialog: MatDialog, private router: Router) { }
ngOnInit() {
let results=[1,2,3]
}
fetchChildrenForPage(page, pagePerPage) {
let results={soemthing: 898, children: [1,2,3,3,4,5,6,7,8,8,8,5,3,3,4]}
this.data = { ...results, children: results.children.slice(1, 5) };
this.createTest(this.data);
}
createTest(clusterInfo): void{
console.log(clusterInfo)
}
}
TreeMapComponent.html(我用于上述组件的html,粗略的想法)
<app-sub-custom-pagination[fetchChildrenForPage]="fetchChildrenForPage">
</app-sub-custom-pagination>
现在我会提到子组件app-sub-custom-pagination
export class SubCustomPaginationComponent implements OnInit {
@Input()
fetchChildrenForPage;
currentPage;
constructor() { }
ngOnInit() {
selectPage(0);
}
selectPage(index): void {
this.currentPage = (index + 1);
this.fetchChildrenForPage(this.currentPage, this.quantityPerPage);
}
但不幸的是我收到了一个错误:
ERROR TypeError: this.createTest is not a function
.
.
.
【问题讨论】:
-
尝试删除它。只调用 createTest(this.data)
-
或者您可以在 subCustomPagination.ts 中使用 @output 指令,而不是使用此 sysntax 在 treeComponent.html 中获取该事件.. (fromSubComponet)= "fetchChildrenForPage"
-
我做不到。这是该方法的小型化形式。 this.createTest 包含另一组 5 6 方法,它们是该父类的一部分。这就是为什么我需要更微妙的解决方案。
-
我同意 GaurangDhorda 的观点。如果要在 TreeMapComponent 中使用子数据,那么您应该使用 @Output 指令。这里讨论了一个类似的话题stackoverflow.com/questions/35328652/…
-
@Output 不是用于孩子与父母的交流吗?我必须完成这项工作,我不能直接调用该 createTest。否则,我现在的解决方案是将此组件直接用作 html 到父组件中,我试图不这样做。