Sébastien Dubois
1 min readMay 26, 2020

--

Hello,

You can find tutorial/example here: https://www.inversionofcontrol.co.uk/injection-tokens-in-angular/

So it's something along the lines of:

export const ROUTER_SCROLL_SERVICE = new InjectionToken<string>('RouterScrollService');

Then in your module, you add it to the providers array like this:

@NgModule({

...

providers: [

{

provide: ROUTER_SCROLL_SERVICE,

useClass: RouterScrollServiceImpl

}

]

...

})

export class AppModule { }

The benefit of this approach is that you can inject the interface (thanks to the injection token) in your components instead of the implementation, meaning that you can easily mock out that dependency for tests.

--

--

Sébastien Dubois
Sébastien Dubois

Written by Sébastien Dubois

PKM Systems Architect Helping Knowledge Workers save 10+ hours/week 1K+ Happy customers ❤️ 🚀 https://developassion.gumroad.com 💌 https://dSebastien.net

No responses yet