簡體   English   中英

我如何將 json 響應 object 的特定數據類型存儲在數組中(例如:數字)以將數據源作為變量傳遞

[英]How can i store the particular data type of the json response object in an array (ex: of numbers) to pass in datasource as variable

export class AppComponent implements OnInit {
    title: String = "Heatmap";
  
    constructor(private app:AppServiceService) { }
    ngOnInit(): void {
        this.app.getdata().subscribe
        (
            (response)=>{console.log(response)}       
        )
    }
   

    dataSource: Object []= [
    [1, this.num.value, 9, 23, 0, 39, 0],
    [0, 18, 37, 0, 0, 50, 0]
    
    ];
    
 
    
   
}

在您的應用程序中的某處聲明類型,例如在您的服務中:

import { Injectable } from "@angular/core";
import { Observable, of } from "rxjs";

export interface GetDataResponse {
    data: number[]
}


@Injectable()
export class AppService {
    public getData(): Observable<GetDataResponse> {
        return of({ data: [1, 2, 3, 4] });
    }
}

然后在您的訂閱參數中使用它:

ngOnInit() {
    this.appService.getData().subscribe((response: GetDataResponse) => {
      console.log(response);
    })
  }

如果響應具有簡單類型,則可以直接使用它:

ngOnInit() {
    this.appService.getData().subscribe((response: {data: number[]}) => {
      console.log(response);
    })
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM