using Injection Tokens in v2 #2291
-
Hi, In v0.x you could use string based injection tokens to create providers and use these strings in providers contructors with @Inject to inject the providers from other modules. This was nice because it meant that you didn't need to have import statements trying to traverse out through your folder structure to other modules to try and include the type def for the providers class to inject it. eg in v0.x I could do the following, where AssetEventsProvider si another provider in same module as this one, so it's a releative path and makes sense to import it (it's in the same folder) and inject it directly. However, my global config is external to this module and sits in with all my service modules that my business api modules all access (things like config, loggers, storage, email providers etc). asset.ts
In v2 I need to do the following in the config provider to generate an injection key, which is all fine. config.ts
But the injection in my consuming provider (ie the asset.ts one above) requires the instance of injection key created in config.ts so I have to import the key from that file anyway. was hoping to something like this:
I was thinking I would just be able to generate a new instace of the injection key inside my asset.ts so that the code can remain decoupled. Am I missing something here? How are others handling this sort of thing, do you store all you keys at top of file structure and just share between modules? Just feels like that goes completely against the point of using a moduler library like this if you are dependant on files outside your module. I mean I'd like to use string based keys for my class based providers as well, not just values. So that in other providers they aren't dependant on importing stuff from all over the file structure that might then change. Also there used to be an example about using string based injection keys for communicating between modules using pubsub in v0.x. Doesn't seem that would be possible now based on the above. Any insights would be greatly appreciated, really hoping I've just completely missed something. Cheers |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, v2 at the moment is using Symbols as injection tokens. It's safer and ensures uniqueness, and reduces the risk of invalid string usages. |
Beta Was this translation helpful? Give feedback.
Hi, v2 at the moment is using Symbols as injection tokens. It's safer and ensures uniqueness, and reduces the risk of invalid string usages.
If using strings is crucial, please open an issue/PR and we'll try to address such support.