0

I need to do some stuff within the route definition, when the lazyloaded module is loaded. The route has a resolver UserDataResolverService, how can I access the resolved data within the route definition?

 {
        path: 'path-a',
        resolve: { userData: UserDataResolverService},
        loadChildren: () =>
            import('./pages/my.module').then((m) => {
                // access resolved data here: userdata.details
                return m.MyModule;
            }),
  }
6
  • 1
    In the lazy loaded module use the ActivatedRoute to get the data. Commented Sep 26, 2022 at 15:26
  • loadChildren should simply return a module. Why would you even want to add logic there? Commented Sep 26, 2022 at 16:43
  • I have a micro-fe architecture. That route loads the remote module and contextually I want to communicate to it some data emitting a CustomEvent @Pieterjan Commented Sep 27, 2022 at 7:07
  • Then either use a service to store the state, or handle the activate event. Don't add complex logic into the route module loader. Commented Sep 27, 2022 at 7:41
  • Note that your very advanced, complex, intensive logic will be called for all the routes when using PreloadAllModules. Your code will result in a horrific UX in that case. (and obviously I strongly suggest you use PreloadAllModules...) Commented Sep 27, 2022 at 7:45

1 Answer 1

1

You can simply inject ActivatedRoute in your child component and then use

constructor(private route: ActivatedRoute) {
    console.log(this.route.snapshot.data['userData']);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I need to access data when the route is defined, not in the component
oh sorry, my bad. Why exactly do you need access there? Is this data used for specifying which modules to load?

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.