New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass configuration data to services in a lazy loaded module #34351
Comments
@kristofdegrave You can use dynamic |
@mlc-mlapis Can you please provide a little example of that, I don't really get what you mean. |
@kristofdegrave Principally like this: #31886 (comment) |
@mlc-mlapis I found that issue before I filed this one. But that thread focuses on loading modules outside the router, it mimics the behavior of what the router does with the advantage you can also inject providers. What I am looking for is a solution with the router. I just want a decoupling between my feature libs and my Applications, but those feature libs need to run inside a context of an application, and that context needs to deliver some configuration to my feature libraries. |
@kristofdegrave OK, using only the router way is a problem. Theoretically, you can nest the one into the other, but it's a question if it's not too complicated. |
I think I found a possible solution on how to handle this. angular/packages/router/src/router_config_loader.ts Lines 47 to 59 in 1e9eeaf
With that in my mind I implemented an own implementation of the Using this implementation I can do the following in my routes:
Some feedback on the solution would be nice, since my knowledge about the injector and compiler is limited, I don't know if I'm doing things I shouldn't. |
Would also be nice for others ending up here looking for a similar solution. I have parent modules lazy loading some feature modules and depending on the parent some things need to be configured differently, would be great to see some feedback on what @kristofdegrave proposed here. |
This seems like something a lot of projects could use. Using Configs/InjectionTokens for lazy loaded routes. So you can keep your feature-modules/libs clean. But somehow it is hard to find any clean solutions. And it is still doesn't seem to be possible with the lazy routes itself => So it makes me wonder.. what are we doing wrong 馃 馃檭 So what would be a proper/better way to solve this? Instead of: TasksModule.forRoot({
config: {
domains: MyDomains,
},
}), doing something like this would be great. [
{
path: 'tasks',
loadChildren: async () => (await import('@my-lib/tasks')).TasksModule.withConfig(
config: {
domains: MyDomains,
},
),
}
] Using a 'global' |
@the-ult Did you mean using
|
Yes. using But the above example does not work like that.. does it? e.g. |
@the-ult Never tried. It's normally a static method declared inside that module. If you tried it and it didn't work, it would mean that it's not invoked in the case of |
Followed this article. Which seems to be working 馃憤 But since it is so difficult to get it done and there is hardly anything found about providing config to a lazy loaded feature module... I'm still wondering what would be a better way to provide @mgechev, or @IgorMinar do you have any solutions/ideas/insights? |
@the-ult That example should be improved a bit to work also in AOT mode and not only in JIT because this code |
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends. Find more details about Angular's feature request process in our documentation. |
Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage. We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package. You can find more details about the feature request process in our documentation. |
I'll close this issue since it didn't collect a sufficient number of votes and doesn't align with an existing project on the roadmap. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
馃殌 feature request
Relevant Package
This feature request is for @angular/core or @angular/router
Description
I have an angular Application and a separate library which contains a feature. This feature is Lazy Loaded.
In that feature I have a service which needs some configuration that is environment dependent. So this information is present in the
environment.ts
files of the application.Now I'm looking for a way to pass this environment information to my lazy loaded feature library. These services should only be accessible inside this module or its decedents.
Describe the solution you'd like
I was thinking about a similar solution as the forRoot or the forChild methods that currently exists.
Describe alternatives you've considered
An alternative solution I currently use is that I have created an addition library with a module that implements a forRoot method. And the module.forRoot() of this library is added to the imports. of the AppModule. Disadvantage of this is that the services is now exposes to the whole application.
An other alternative solution could be to inject a AppSettings service into the service living in the feature library and fetch the environment settings of this service. Disadvantage of this solution is that for every feature library I additionally add I have a tight coupling to that service.
The text was updated successfully, but these errors were encountered: