Sitemap
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

Leading the movement to transform mental chaos into clarity, helping knowledge workers break free from information overload. 💌 https://dsebastien.net

No responses yet