一旦 UI 在 Angular8 中完全渲染,如何进行一些操作
我只想在 UI 完全渲染后执行一些操作。
由于子组件来自库,因为未定义的
子组件来自库(来自另一个应用程序的组件作为依赖项添加)。所以我无法修改/增强子组件
my.html
<div>
<h4>Parent</h4>
<child-component #childComponent></child-component>
</div>
my.component.ts
@ViewChild('childComponent', {read: ChildComponent, static: false}) private child: ChildComponent;
private myFunction(): void {
console.log('child', this.child); // This prints undefined.
if(this.child) {
// DO some operations
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码已经足够好了,你只需要在子进程加载时触发
myFunction()
即可,这可以从 Angular 的ngAfterViewInit
生命周期钩子中进行检查:检查 https://stackblitz.com/edit/angular-parent-to-child-communication-avxqor 我已将 var 添加到子组件中,只是为了表明可以通过这种方式访问它,并添加了一个控制台。登录子进程的 OnInit 以显示子进程在父进程的
ngAfterViewInit
挂钩之前启动。Your code is good enough, you just need to trigger
myFunction()
when the child is loaded, and that can be checked from Angular'sngAfterViewInit
lifecycle hook:Check https://stackblitz.com/edit/angular-parent-to-child-communication-avxqor I've added var into child component just to show that it's accessible this way, and added a console.log on child's OnInit to show that the child is initiated before parent's
ngAfterViewInit
hook.