I am creating a shared library for Angular 2 projects. I can add classes and UI components to the library and use them in another project without a problem. However, when I create a service provider, I am unsure of how to inject for example the http dependency.
I made a demo shared lib here to provide an example of what I am doing. This lib is linked (via npm link) to another project (an Ionic 2 starter project).
I have created a sample class in the Socius shared lib to make an http call to Wikidata. A normal constructor for a provider class which I would use from a regular project would be like this:
@Injectable()
export class Wikidata {
constructor(public http: Http)
Using it this way creates a runtime error: Can't resolve all parameters for TutorialPage: (NavController, MenuController, TranslateService, ?).
I also saw some example using this syntax:
constructor(@Inject(Http) public http: Http) {
This will generate the same runtime error.
Following an answer to this StackOverflow question I tried this:
private wikidata: Wikidata;
constructor(injector:Injector) {
setTimeout(() => this.wikidata = injector.get(Wikidata));
}
Using this method causes a new error:
EXCEPTION: Token must be defined!
What is the correct way to use a class that has a dependency like this?