0

Is there any diffrence between selecting store into the constructor and before it?

1) export class SomeClass {
      myObservable$ = <Observable<MyObservableType>>this.store.select(fromReducers.getMyObservable);
      constructor(
        private store: Store<fromReducers.State>
      ) { }
    } 

2) export class SomeClass {
      myObservable$ = Observable<MyObservableType>;
      constructor(
        private store: Store<fromReducers.State>
      ) {
        this.myObservable$ = this.store.select(fromReducers.getMyObservable);
      }
    }

And why in the ngrx/store app-example they use second variant?

mr__brainwash
  • 1,334
  • 3
  • 16
  • 40
  • After some looking around I found [this](https://stackoverflow.com/questions/36147890/how-does-variable-declaration-differ-between-the-class-and-constructor) post which goes over the differences. Personally I prefer the second method because it's easier to understand. – Joseph Webber Jun 14 '18 at 13:50
  • In angular 6 you can pass a param to constructor and it's scope is all the class instance – firegloves Jun 14 '18 at 14:12

1 Answers1

0

The difference is that in the first case it request for data during class instantiation with the risk of slow down the initialization process of all the web application and with the risk of destroy user experience.

Long operations should not go into constructors nor into variable declarations.

The best option is to fetch data into ngOnInit method. Same thing for all potentially long operations.

Look at angular lifecycle docs to have a deepest idea of what is happening behind the scenes

It's a good practice to initializate variables at declarations point, but is not mandatory to init them with real values. Generally they are initialized to null and then they get their values in the constructor or after the constructor because by this way you should be sure that all other variables from which you could depends on should be instantiated.

But this one is not a rule, it depends on what your class does

firegloves
  • 5,581
  • 2
  • 29
  • 50
  • That's not what he's asking. – Joseph Webber Jun 14 '18 at 12:57
  • and what he's asking for? – firegloves Jun 14 '18 at 12:58
  • Look where he's declaring `myObservable$`, he's wondering what the difference is between initializing it with `this.store` inside the constructor as opposed to outside the constructor. – Joseph Webber Jun 14 '18 at 13:01
  • it seems to me that he said store.select, that is not a declaration, am I wrong? – firegloves Jun 14 '18 at 13:04
  • Ok, so what's the difference? – firegloves Jun 14 '18 at 13:11
  • I mean in first variant i ma assigning observable before constructor. And in second - inside. And i am not fetching data. It is just selector. – mr__brainwash Jun 14 '18 at 14:45
  • the result is almost the same. if you want to discover precisely the difference you can debug it into your browser and see which statement is executed first. If the operation is speedy and you don'y need data during class instantiation the preferred way should be using ngOnInit, otherwise you can also use one of that init forms – firegloves Jun 14 '18 at 14:49