【问题标题】:Error TS2339: Property 'JSON' does not exist on type 'Object'. ".map(resp => resp.JSON())" angular 4错误 TS2339:“对象”类型上不存在属性“JSON”。 “.map(resp => resp.JSON())” 角度 4
【发布时间】:2018-09-14 21:26:07
【问题描述】:

在我的文件contacts.service.ts 中。我有自己的代码

import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";

@Injectable

export class contactsService{

constructor(public http: HttpClient){

}

getContact(){
     return this.http.get('http://localhost:8080/chercherContacts? 
  mc=sa&page=2&size=6')
     .map(resp => resp.JSON());


 }

}

我的函数 getContact() 有问题,因为 HttpClient 不想要地图,我收到消息并收到消息:Property 'JSON '在类型'对象'上不存在

当我忘记此函数中的 .map 时如何修改此代码

如果我可以在我的项目中更正此代码并考虑所有问题,请给我建议。

【问题讨论】:

    标签: angular angular-services angular-service-worker


    【解决方案1】:

    使用 Angular HttpClient 4.x+,默认情况下它会将 JSON 响应解析为对象,您无需像使用 2.x 及更低版本那样针对响应执行 json()。你可以这样做:

    getContact() {
      return this.http.get('http://localhost:8080/chercherContacts?mc=sa&page=2&size=6');
    }
    

    如果不是 JSON,你只需要指定一个responseType

    getContact() {
      return this.http.get('http://localhost:8080/chercherContacts?mc=sa&page=2&size=6', {responseType: 'text'});
    }
    

    此外,请查看有关 Typechecking the response 的 4.x 文档,您可以在其中指定一个接口来告诉 HttpClient 响应的类型。这是推荐的,它将在消费组件/服务/等中提供打字支持:

    SomeInterface {
     foo: number;
     bar: string;
    }
    
    getContact() {
      return this.http.get<SomeInterface>('http://localhost:8080/chercherContacts?mc=sa&page=2&size=6');
    }
    

    这是一个简单的example 的实际操作。

    希望对您有所帮助!

    【讨论】:

      【解决方案2】:

      在我的应用程序中。我在此文件夹中的目录 services 中有一项服务,当我将 contact.service.ts 放入此服务时,我有我的第一个服务

      import {HttpClient} from "@angular/common/http";
      
      @Injectable
      
      export  class ContactsService {
      
      constructor( public http: HttpClient){
      
      }
      
      getContacts(){
        return this.http.get("http://localhost:8080/findPersons?mc=wa&page=1&size=6");
      }
      
      }
      

      在我的组件 contact.component.ts 中,每当我在此文档中编写此代码时,我都希望收到此 url

      import { Component, OnInit } from '@angular/core';
      import {HttpClient} from "@angular/common/http";
      import {ContactsService} from "../../services/contact.service";
      
      @Component({
      selector: 'app-contact',
      templateUrl: './contact.component.html',
      styleUrls: ['./contact.component.css']
      })
      export class ContactComponent implements OnInit {
      
      pageContacts: any;
      
      constructor(public http: HttpClient, public contactService: ContactsService) { }
      
      ngOnInit() {
      
      this.contactService.getContacts().subscribe(data => {
        this.pageContacts = data;
      }, err => {
        console.log("L'erreuR : "+err);
      });
      
       }
      
      }
      

      但是我有一个错误错误在 src/services/contact.service.ts(4,1): 错误 TS1238: Unable to resolve signature of class decorator when called as an expression.

      ligne(4,1) 在 @Injectable 我不知道问题出在哪里

      我的问题是删掉这段代码

      ngOnInit() {
      this.http.get('http://localhost:8080/findPersons?mc=wa&page=1&size=6')
        .subscribe(data =>{
          this.pageContacts = data;
        }, err => {
          console.log('ErreuR : '+err);
        });
      
      }
      

      在两个。当我收到 url 时的一项服务和第二项用于在页面 web 中写入数据

      仅此而已

      【讨论】:

        【解决方案3】:

        角度的版本将在我的项目中失效 当我运行我的应用程序时,我会关注此页面

        Angular Injectable decorator - Expected 0 arguments but got 1

        感谢我在这个堆栈中的所有朋友 :)

        【讨论】:

          猜你喜欢
          • 2020-07-07
          • 2018-07-16
          • 1970-01-01
          • 2018-05-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多