[英]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]
];
}
Declare the type somewhere in your app, for example in your service:在您的应用程序中的某处声明类型,例如在您的服务中:
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] });
}
}
Then use it in your subscribe parameter:然后在您的订阅参数中使用它:
ngOnInit() {
this.appService.getData().subscribe((response: GetDataResponse) => {
console.log(response);
})
}
If the response has a simple type, you can just use it directly:如果响应具有简单类型,则可以直接使用它:
ngOnInit() {
this.appService.getData().subscribe((response: {data: number[]}) => {
console.log(response);
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.