0

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

Mike Mameko
  • 199
  • 1
  • 2
  • 11

1 Answers1

-1

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

Mike Mameko
  • 199
  • 1
  • 2
  • 11
  • Or create it in a separated file. For example https://github.com/angular/angular/blob/master/packages/core/src/application_tokens.ts – yurzui Nov 19 '17 at 12:07