angular - Uncaught TypeError: this.jsonp.request is not a function in Angular2 -
i getting following error. uncaught typeerror: this.jsonp.request not function in angular2. can please me fix that.
my code this:
requestservice.ts
import {component} '@angular/core'; import {jsonp_providers, jsonp,requestmethod, requestoptions, urlsearchparams,jsonp_bindings, baserequestoptions} '@angular/http'; import {injectable} '@angular/core'; import 'rxjs/add/operator/map'; @injectable() @component({ providers: [jsonp_providers] }) export class requestservice { constructor(public jsonp?:jsonp) { this.jsonp = jsonp; } getvalues = (url) => { let _urlparams = new urlsearchparams(); _urlparams.set('contenttype', 'application/jsonp; charset=utf-8'); _urlparams.set('datatype', 'jsonp'); _urlparams.set('timeout', '5000'); this.jsonp.request(url + "&callback=jsonp_callback", { // getting error here contenttype: "application/jsonp; charset=utf-8", datatype: 'jsonp', timeout: 5000 }) .map(res => { return res.json(); }) .subscribe(res => { // here stuffs response }), err => { console.log('error') }; } }
abc.ts
import {requestservice} './../request_service'; export class abc { private request:any = new requestservice(); bindvalues() { this.request.getvalues('http://google.co.in'); // note: url given here sample } }
thanks in advance
the problem @ line:
constructor(public jsonp?:jsonp) { this.jsonp = jsonp; // <---- }
with public or private keyword, don't need set property. set class , not constructor parameter...
try following:
constructor(public jsonp?:jsonp) { }
edit
i think have problem decorators. in case of service, need @injectable
one, not @component
1 (only components).
so use following:
@injectable() export class requestservice { constructor(public jsonp?:jsonp) { } (...) }
set providers when bootstrapping application:
bootstrap(appcomponent, [ jsonp_providers ]);
see question:
to understand how inject dependencies services, have @ question describing how hierarchical injectors work:
Comments
Post a Comment