I want to use different value for a injection token in a module conditionally, something like:
providers: [
{
provide: ABC_TOKEN,
useValue: useA ? a : b,
},
],
how to do that?
and also I want to pass the useA
from a routing module which maybe something like:
{
path: 'abc',
loadChildren: () =>
import('../abc.module').then(
(m) => m.AbcModule,
),
data: {
useA: true,
},
},
So what I want is based on the data in the routing module, I want to use different value of injection token for the module
Thanks.
If you wanted to decide the value of
ABC_TOKEN
injection token, consider usinguseFactory
function. And define that in yourAbcModule
module.On catch is, you have to access the parent route
data
from the current activatedRoute. For that, you may have to traverse. or two steps back usingroute.firstChild.firstChild.snapshot.data['useA']
. Also, this can be written using a recursive function.I've created a Demo for you on this. Navigate to the product-list page and check code inside
ProductModule
Stackblitz