I would like to inject an implementation of an interface into an Ionic2 component (Ionic2 Beta 10, uses Angular2 RC4). Lets say we have the following service:
export interface ServiceInterface { /*...*/ }
@Injectable()
export class ServiceImpl implements ServiceInterface { /*...*/ }
How do I inject that into an Ionic2 component? I have tried:
import { Component, provide, Provider } from '@angular/core';
import { ServiceInterface } from './service.interface';
import { ServiceImpl } from './service.impl';
@Component({
providers: [
// Exception: Can't resolve all parameters for SomeComponent: (?)
ServiceImpl,
// @deprecated - seen on: http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html
provide(ServiceInterface, {useClass: ServiceImpl}),
// @deprecated - seen on: http://plnkr.co/edit/RSTG86qgmoxCyj9SWPwY?p=preview
new Binding(ServiceInterface, {toAlias: ServiceImpl}),
// @deprecated - seen on: https://www.dartdocs.org/documentation/angular2/2.0.0-beta.9/angular2/Provider/useClass.html
new Provider(ServiceInterface, {useClass: ServiceImpl}),
// TS Error: Cannot find name 'ServiceInterface' - even though it is properly referenced
{provide: ServiceInterface, useClass: ServiceImpl}
]
})
export class SomeComponent {
constructor(private service: ServiceInterface) {}
}
Simply using the ServiceImpl
as follows works perfectly fine, but that is not what I want:
@Component({providers: [ServiceImpl]})
export class SomeComponent {
constructor(private service: ServiceImpl) {}
}
Any ideas what I am missing?