'Object' 类型的参数不可分配给 'JSON' Httpclient GET 类型的参数

Argument of type 'Object' is not assignable to parameter of type 'JSON' Httpclient GET

您好,我正在开发 Angular 6 + Flask 应用程序,我遇到了这个问题:

error TS2345: Argument of type 'Object' is not assignable to parameter of type 'JSON'.

当我的代码这样做时:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { MessageService } from '../shared/message.service';

@Component({
  selector: 'app-network-monitorer',
  templateUrl: './network-monitorer.component.html',
  styleUrls: ['./network-monitorer.component.css']
})
export class NetworkMonitorerComponent implements OnInit {

  mqttMessageData : JSON[]=[];
  coapMessageData : JSON[]=[];
  xmppMessageData : JSON[]=[];

  constructor(private httpClient: HttpClient, private messageService:MessageService) { }

  ngOnInit() {
    setInterval(()=>{
      this.getMqttMessages(); },2000);
  }

  getMqttMessages() {
    this.httpClient.get('http://127.0.0.1:5002/messages/mqtt').subscribe(data => {
      this.mqttMessageData.push(data);
      console.log("message :")
      console.log(this.mqttMessageData.length())
      console.log(data);
    });
  }

所以基本上当组件正在加载时,我向我的 python 服务器发出请求以获取一些数据,这些数据在 JSON 中返回给客户端,但 angular 似乎认为'data' 是对象类型,所以我无法将它添加到我的 JSON

列表中

你必须施放它:

this.httpClient.get<JSON[]>('http://127.0.0.1:5002/messages/mqtt').subscribe(data => {

您需要将数据转换为 JSON。 尝试使用 data.json,如下所示。

getMqttMessages() {
this.httpClient.get('http://127.0.0.1:5002/messages/mqtt').subscribe(data => {
  this.mqttMessageData.push(data.json);
  console.log("message :")
  console.log(this.mqttMessageData.length())
  console.log(data);
});
}