ngxsOnInitmethod will be invoked after all the states from the state's module definition have been initialized and pushed into the state stream. The states'
ngxsOnInitmethods are invoked in a topological sorted order going from parent to child. The first parameter is the
StateContextwhere you can get the current state and dispatch actions as usual.
ngxsAfterBootstrapmethod will be invoked after the root view and all its children have been rendered, because Angular invokes functions, retrieved from the injector by
APP_BOOTSTRAP_LISTENERtoken, only after creating and attaching
ComponentRefof the root component to the tree of views.
ngxsOnInit()and whenever state changes.
ngxsOnChanges()and before the
APP_INITIALIZERtoken is resolved.
APP_INITIALIZERis just a token that references Promise factories. If you've ever used the
APP_INITIALIZERtoken, then you are already familiar with its syntax:
ApplicationInitStatusclass. What does Angular do under the hood? It gets an instance of this class and invokes the
APP_INITIALIZERarray (as it's a
multitoken) and invoking those factories, that you've provided in
asyncInitPromisesare provided into
Promise.all. That's all the magic. That's why the
APP_INITIALIZERis resolved after NGXS states are initialized. They are initialized by the
NgxsModulethat is imported into the
ngxsOnInitmethod on states is also invoked before the
APP_INITIALIZERtoken is resolved. Given the following code:
getVersionmethod is invoked before the
versionproperty is set. Why? Because the
ngxsOnInitmethods on states are invoked before the
SetVersionaction right after the version is fetched:
ngxsOnInitmethods that is fetched during the