When it comes to initializing complex properties like observables, form-related elements, or synchronous code that requires time and resources, the question arises: Where is the best place to do this?
Consider having the same component in two different versions:
@Component({})
class ExampleComponent {
// Initialize in class body or constructor
users$ = this.store.select(selectUsers);
constructor(
protected store: Store<any>,
) { }
}
@Component({})
class ExampleComponent implements OnInit {
users$: Observable<User[]>;
constructor(
protected store: Store<any>,
) { }
// Initialize on component init
ngOnInit() {
this.users$ = this.store.select(selectUsers);
}
}
Which version is more efficient? What are the advantages and disadvantages of each approach?