错误TS2339:类型“对象”上不存在属性“ JSON”。 “ .map(resp => resp.JSON())”角度4

时间:2018-09-14 21:26:08

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

在我的文件contact.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 '在'Object'类型上不存在

当我忘记此功能中的 .map 时,如何修改此代码

当我可以在项目中更正此代码并考虑全部内容时,请给我建议。

2 个答案:

答案 0 :(得分:0)

在我的应用程序中。我在此文件夹中的目录服务中有一项服务,当我将 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:作为表达式调用时无法解析类装饰器的签名。

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

我的问题是削减此代码

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

}

一分为二。我收到网址时的一项服务,第二项是在网页中写入数据的服务

就这些

答案 1 :(得分:0)

Angular的版本会在我的项目中引起争议 运行我的应用程序时,我会遵循此页面

Angular Injectable decorator - Expected 0 arguments but got 1

感谢我所有的朋友在此叠加中:)