[英]Does it make sense to wrap jQuery code in `document.ready()` inside your Angular 2 components functions
I simply want to be sure that if the Angular component code is running. 我只是想确定如果Angular组件代码正在运行。 It means at that moment the DOM
should be ready. 这意味着在那一刻DOM
应该准备好了。 Example:: TypeScript 示例:: TypeScript
export class SomeComponent{
constructor(){
$('body').do();
}
}
OR 要么
export class SomeComponent{
constructor(){
$(document).ready(function(){
$('body').do();
}
}
}
Add your code to ngAfterViewInit()
of the root component AppComponent
. 将代码添加到根组件AppComponent
ngAfterViewInit()
。 When this lifecycle methos is called, you can be sure the DOM is ready. 调用此生命周期方法时,可以确保DOM已准备就绪。 The document.ready
event will have been sent way before this lifecycle method is called. 在调用此生命周期方法之前,将已发送document.ready
事件。
See also https://angular.io/docs/js/latest/api/core/AfterViewInit-interface.html 另请参阅https://angular.io/docs/js/latest/api/core/AfterViewInit-interface.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.