简体   繁体   English

将jQuery代码包装在Angular 2组件函数内的`document.ready()`中是否有意义

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM