typescript - Angular 2 injected service into another service is undefined -
i'm trying create small framework make easier me implement crud functionality.
therefore i've created basehttpservice this:
... <imports> ... @injectable() export class basehttpservice { constructor( private http: http ) { console.log('ctor'); this.apiurl$.subscribe(apiurl => this.rootapiurl = apiurl.removetrailingslash()); } public getpagedresultoftype<t extends ibaseentity>(url: string, vm: isearchvm<t>, options?: requestoptions): observable<pagedresult<t>>{ ... <implementation> ... } ... <other methods> ... }
then have baseentityreadonlyservice uses basehttpservice create read fucntionality of crud:
@injectable() export class baseappsumentityreadonlyservice<t extends ibaseentity>{ constructor( @inject(forwardref(() => basehttpservice)) public basehttpservice: basehttpservice // public basehttpservice: basehttpservice ) { console.log('ro ctor'); console.log(this.basehttpservice); // <-- undefined } getall(vm: isearchvm<t>): observable<pagedresult<t>>{ const url = `${this.baseurl}`; console.log(this.basehttpservice); return this.basehttpservice.getpagedresultoftype<t>(url, vm); // <-- fails because basehttpservice undefined: cannot read property 'getpagedresultoftype' of undefined } ... <more methods> }
in frameworkmodule, i've tried alot:
@ngmodule({ declarations: [ ... ], imports: [ commonmodule, httpmodule ], providers: [ basehttpservice, // baseappsumentityreadonlyservice { provide: baseappsumentityreadonlyservice, useclass: baseappsumentityreadonlyservice, deps: [ basehttpservice ] }, ] }) export class appsumframeworkmodule { }
this have, not work. error is:
cannot read property 'getpagedresultoftype' of undefined
how can basehttpservice injected in baseappsumentityreadonlyservice?
import service this:
import { basehttpservice} '../servicepath';
then declare in constructor:
constructor( private basehttpservice: basehttpservice ) { console.log(this.basehttpservice); }
let me know if works this, , if not, copy/paste console error please. :d
edit 1:
service: basehttpservice
... <imports> ... @injectable() export class basehttpservice { constructor( private http: http ) { console.log('ctor'); this.apiurl$.subscribe(apiurl => this.rootapiurl = apiurl.removetrailingslash()); } public getpagedresultoftype<t extends ibaseentity>(url: string, vm: isearchvm<t>, options?: requestoptions): observable<pagedresult<t>>{ ... <implementation> ... } ... <other methods> ... }
service: baseappsumentityreadonlyservice
@injectable() export class baseappsumentityreadonlyservice<t extends ibaseentity>{ constructor( private basehttpservice: basehttpservice ) { console.log('ro ctor'); console.log(this.basehttpservice); // <-- undefined } getall(vm: isearchvm<t>): observable<pagedresult<t>>{ const url = `${this.baseurl}`; console.log(this.basehttpservice); return this.basehttpservice.getpagedresultoftype<t>(url, vm); // <-- fails because basehttpservice undefined: cannot read property 'getpagedresultoftype' of undefined } ... <more methods> }
module:
@ngmodule({ declarations: [ mycomponent ], imports: [ commonmodule, httpmodule ], providers: [ basehttpservice, baseappsumentityreadonlyservice, ] }) export class appsumframeworkmodule { }
component: mycomponent
... <imports> ... @component() export class mycomponent { constructor( private baseappservice: baseappsumentityreadonlyservice ) { console.log(this.baseappservice); } }
Comments
Post a Comment