【发布时间】:2016-08-09 14:02:17
【问题描述】:
我收到以下错误。 未捕获的 TypeError:this.jsonp.request 不是 Angular2 中的函数。谁能帮我解决这个问题。
我的代码会是这样的:
-
RequestService.ts
import {Component} from '@angular/core'; import {JSONP_PROVIDERS, Jsonp,RequestMethod, RequestOptions, URLSearchParams,JSONP_BINDINGS, BaseRequestOptions} from '@angular/http'; import {Injectable} from '@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 some stuffs with response }), err => { console.log('error') }; } } -
abc.ts
import {RequestService} from './../request_service'; export class ABC { private request:any = new RequestService(); bindValues() { this.request.getValues('http://google.co.in'); // note: url given here is sample } }
提前致谢
【问题讨论】:
-
你用的是哪个版本的Angular2?
-
Angular 版本 2.0.0-rc.1 @Thierry Templier