你可以试试这样做吗?
使用类型选择器:
子组件
@Component({
selector: 'app-acknowledge-notice-modal',
template: <div>...</div>
})
class AcknowledgeNoticeModalComponent{
showAcknowledgement() {
this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => {
data.data.filter((res: any) => {
if (data.isSuccessful === true) {
this.noticeText = res.noticeText;
this.noticeAttachment = res.noticeAttachement;
this.noticeCode = res.noticeCode;
} else if (data.isSuccessful === false) {
this.message = [{ field: "", message: data.message[0].message }];
this.objError = this.message;
this.showError = true;
window.scrollTo({ top: 0, behavior: 'smooth' });
}
})
});
}
}
父组件
@Component({
selector: parent-component,
template: <app-acknowledge-notice-modal></app-acknowledge-notice-modal>,
directives: [AcknowledgeNoticeModalComponent]
})
class ParentComponent{
@ViewChild(AcknowledgeNoticeModalComponent) child: AcknowledgeNoticeModalComponent;
ngAfterViewInit() {
this.child.showAcknowledgement();
}
}
使用字符串选择器:
子组件
@Component({
selector: 'app-acknowledge-notice-modal',
template: <div>...</div>
})
class AcknowledgeNoticeModalComponent{
showAcknowledgement() {
this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => {
data.data.filter((res: any) => {
if (data.isSuccessful === true) {
this.noticeText = res.noticeText;
this.noticeAttachment = res.noticeAttachement;
this.noticeCode = res.noticeCode;
} else if (data.isSuccessful === false) {
this.message = [{ field: "", message: data.message[0].message }];
this.objError = this.message;
this.showError = true;
window.scrollTo({ top: 0, behavior: 'smooth' });
}
})
});
}
}
父组件
@Component({
selector: parent-component,
template: <app-acknowledge-notice-modal #child></app-acknowledge-notice-modal>,
directives: [AcknowledgeNoticeModalComponent]
})
class ParentComponent{
@ViewChild('child') child: AcknowledgeNoticeModalComponent;
ngAfterViewInit() {
this.child.showAcknowledgement();
}
}