Solvedangular 4-10 seconds delay between navigated component's Constructor and ngOnInit()
✔️Accepted Answer
I have also faced this behavior. Following workaround works for me:
constructor (
private zone: NgZone,
private changeDetector: ChangeDetectorRef
) {
setTimeout(() => {
this.zone.run(() => {
this.changeDetector.detectChanges();
});
});
}
Very similar to @tytskyi suggestion.
I know that putting zone inside timeout may look weird. This is my result of trial and error process. Looks like this task has to be put on queue first and then wrapped inside zone.
Caveats:
- because we are manually rushing change detection, the component might have some problems with
@Input
s. I had to assign default values to my inputs in order to make it work.
@Input() public site: Site;
changed to
@Input() public site: Site = {};
- I also had to move all my observable definitions from
ngOnInit
toconstructor
(same reason as above).
constructor() {}
public onOnInit(): void {
this.selectedInstance$ = this.selectedInstanceSubject$
.distinctUntilChanged()
}
changed to
constructor() {
this.selectedInstance$ = this.selectedInstanceSubject$
.distinctUntilChanged()
}
public onOnInit(): void {}
Looking forward to get it sorted.
Other Answers:
Thanks @tytskyi
Indeed, calling ChangeDetectorRef markForCheck() + detectChanges() "fixes" the situation:
constructor(private cdr: ChangeDetectorRef) {
setTimeout(() => this.setChanged(), 0);
}
setChanged() {
this.cdr.markForCheck();
this.cdr.detectChanges();
}
Without setTimeout(), everything breaks.
One of the problems with this workarounds is that we would need to repeat this in pretty much all components that can be possibly navigated to, which is plenty of components.
Same problem here... in my case this behaviour just happens when I'm logged with google auth, when logged with firebase email/pwd I have no delay.
It's my first project with angular, so I don't know if I'm doing something stupid. But the cited workaround works fine for me too. Thanks.
refresh() {
setTimeout(() => {
this.zone.run(() => {
this.cdr.detectChanges();
});
});
}
I'm submitting a...
Current behavior
Initial load of the module happens quickly but, when navigated to a route, the constructor of the destination component is also called in a timely manner, but the ngOnInit() method is not called for another 4-10 seconds. This happens every time and with not just one but all components that are called using navigation.
Expected behavior
This issue only surfaced when I upgraded Angular from 4.0.1 to 4.3.4. No code was changed in the app.
Minimal reproduction of the problem with instructions
I'm unable to create a plunker to reproduce this issue with shell code. If someone can shed some light on what happens between the component's constructor being called and ngOnInit() being called, I may be able to debug this further.
EDIT- Notably, the delay is observed right after processing Google Auth response that is processed by a Service, which then calls router.navigate() to the component in question.
What is the motivation / use case for changing the behavior?
The motivation is obvious. The delay is not expected behavior.
Environment
Production code is built using the following command -
node --max_old_space_size=8192 ./node_modules/.bin/ng build --aot --prod