Mike Mameko
Mike Mameko

Reputation: 319

Angular: Inject an opaque token

I try to understand how I could inject an opaque token.

For instance, I have some service MyService, and I create an opaque token for it:

import {OpaqueToken} from '@angular/core'; 
...
const serviceToken = new OpaqueToken('MyService');
...
providers: [
    {
        provide: serviceToken,
        useValue: 'Service value!'
    }
]

It's ok. But how I could inject this service into my component?

I tried to create the same instance of an Opaque Token in my component and inject it but I got an error: Error: No provider for Token MyService!

Please, you could review an example: Plunker

Upvotes: 0

Views: 292

Answers (1)

Mike Mameko
Mike Mameko

Reputation: 319

Conclusion: We could use the same instance of an opaque token in component. Just export it from the module and use in a component.

Upvotes: -1

Related Questions