title: Pass data to lazy loaded module and make available to services without going through components
body: <p>I have a lazy loaded module I need to pass some config data into.</p>
  
  <p>I know you cannot use forRoot for lazy loaded modules.</p>
  
  <p>I have tried passing data in the route, which works, but as far as I know the only way to fetch it in the lazy loaded module is from the ngOnInit method of the root component. The problem is that I need this data in an API service that is instantiated and used before the root component of the lazy loaded module is initialised, so by the time ngOnInit is called, it is too late. For example, the API service is called from resolvers, which are executed before the component is initialised.</p>
  
  <p>Is there a way of getting the data form the route from a service in the lazy loaded module or passing it some other way? I would like this module to be standalone so I cannot inject application-level services into it. The data needs to be passed in dynamically.</p>
  
  <p>What I have tried:</p>
  
  <p>Pass data into the lazy loaded module from the root routing module</p>
  
  <p>//app/app-routing.module.ts</p>
  
  <pre><code>const appRoutes: Routes = [
     {
          path: 'feature',
          loadChildren: 'feature#featureModule',
          data: {
              test: 'someData'
          }
      },
  ]
  </code></pre>
  
  <p>In the lazy loaded module, I can fetch the data from the root component</p>
  
  <p>// feature/root.component.ts</p>
  
  <pre><code>export class FeatureRootComponent {
  
      constructor(private activatedRoute: ActivatedRoute,
                ) {
  
          this.activatedRoute.data.pipe(
              take(1)
          ).subscribe(data =&gt; {
             console.log(data.test); // works fine
          });
  
  }
  </code></pre>