使用方法map()时无法解析的函数

时间:2019-01-19 05:00:12

标签: angular typescript http

无法解析功能map()。我也尝试导入rxjs / add / operator / map

getVenues() {
if (this.placeValue != null && this.placeValue != "" && this.recipeValue != null && this.recipeValue != "") {
  this._http.get("https://api.foursquare.com/v2/venues/search" +
    "?client_id=3PPNMTIKJJNDVYPFOBGSHHV2PR5A2P05PYHXDN2MKSKTTBSX" +
    "&client_secret=0QPHT0F5RS043F4TB4KKPQSHKSAXKE5ZNOYGB5KL2MBDYAG4" +
    "&v=20160215&limit=5" +
    "&near=" + this.placeValue +
    "&query=" + this.recipeValue)
    .map(res => res.json())
    .subscribe(data => {
      // we've got back the raw data, now generate the core schedule data
      // and save the data for later reference
      this.data1 = data.results[0].key;
      console.log(this.data1)},
      err => console.log(err)

      );

如果我使用HttpClientModule而不是HttpClient,则会出现错误

  

无法解析方法get()

1 个答案:

答案 0 :(得分:1)

通常在使用pipe()时使用httpclient

this.http.get(url).pipe(map(a => b), catchError(error => this.handleError(error)));

就像对httpClient.get()的评论一样:

/**
 * Construct a GET request which interprets the body as JSON and returns it.
 *
 * @return an `Observable` of the body as type `T`.
 */

由于它返回一个Obervable,因此您将按照pipe将功能合并到一个链中。

提示

  • 您应该在JS中使用!=====以避免产生奇怪的结果;
  • 通常我们不会在subscribe之后直接get,而是将get方法放入单独的 service ;