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?

3
  • 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. Commented Jul 13, 2018 at 8: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. Commented Jul 13, 2018 at 9:07
  • That's right @adrian-fâciu, simply will create a header component... thanks! Commented Jul 13, 2018 at 9:09

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.