大家好我正在研究Angular应用程序,我想帮助从json获取数据。我用我的方法得到了我的conpoments.ts但它没有用。我包含了json文件。请帮我在html页面上显示数据。谢谢
label: "car"
manufacture:Array(3)
{id: "1", label: "name", question: "", type: "type", value: {…}}
{id: "2", label: "name", question: "", type: "type", value: {…}}
{id: "3", label: "name", question: "", type: "type", value: {…}}
import { Component, OnInit } from '@angular/core';
import { ManufactureService } from '../../services/ManufactureService/manufacture.service';
@Component({
selector: 'body-content',
templateUrl: './body.component.html',
styleUrls: ['./body.component.less']
})
export class BodyComponent implements OnInit {
constructor(public
manufactureService:ManufactureService) { }
async ngOnInit() {
const [err,data] = await this. manufactureService.getManufature('car','man').toPromise()
if (err) {
console.log(err)
return false
}
showCar(data){
data.forEach(el=>{
this.manufactureService.getManufature.push();
})
}
}
}
答案 0 :(得分:0)
Try This :
export class MyComponent implements OnInit {
constructor(private manufactureService: ManufactureService) {}
ngOnInit(): void {
this. manufactureService.getManufature(subscribe(data => {
console.log("results :", data)
data.forEach(el=>{
this.manufactureService.getManufature.push();
})
},error => {
console.log("Error :", error)
});
}
}
服务功能:
// import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) {}
getManufature(){
return this.http.get('path or url to JSON');
}