2

I have just discovered the resolver class, and find it very useful.

There are different objects I need to preload, depending on specific url.

However, in my layout (app.component.ts + app.component.html) I would like to use resolver in order to preload logged user data from api.

I know this component is not accessed via router, so at the moment I use ngOnInit hook.

Is there any way to use resolver to preload data before the app component is rendered?

guyaloni
  • 4,972
  • 5
  • 52
  • 92
  • 1
    Usually in the app component one would have only the basic layout and the router outlet. So if you would create a component that needs the data from the api and navigate to it you should be able to use the approach you want. – Adrian Fâciu Jul 13 '18 at 08:28
  • **1.** you need to implement the resolver interface to a service. **2.** put this resolver service to the particular route. **3.** on the routed component you should take the ActivatedRoute to subscribe to the data it has. – Jai Jul 13 '18 at 09:07
  • That's right @adrian-fâciu, simply will create a header component... thanks! – guyaloni Jul 13 '18 at 09:09

1 Answers1

3

APP_INITIALIZER is more suited to loading data for the whole app.

Since Angular 12 you can use Observables.

For app.component it looks like the resolver does run and waits for it to complete, but it just doesn't pass the data to the App component. However, that data is available to child components even if the resolve is defined at the top level. But APP_INITIALIZER is probaly your best bet if something truly needs to be loaded globally!

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689