简体   繁体   中英

NgOnInit called twice

I have a page that is behind a guard, and on the ngOnInit I usually do a subscription to a context management server that consists of first a post to create a subscription then opening a websocket connection with the server. On my local copy everything seems to work fine... But recently i've noticed in the production build it sometimes does the subscription twice which leads me to believe that ngOnInit is being called twice. I'am not sure why this is happening.

At the moment my solution was to just move this subscription code to a service and have it called once from app components ngOnInit.

Turns out I was using a Auth lib and i had the post_login url and the redirect url poiniting to a redirection page, turns out one was supposed to be a redirect and the other is used as a page to load once log in is complete, so after pointing post_login_url to "/" it seemed to fix the issue. Though the one question in my mind is, even though 2 redirects were happening, why would that trigger ngOnInit twice and like the same component was loaded twice on one page? Shouldn't it be as if the page had just reloaded instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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