我正在尝试将app.component文件中的console.log数据添加到服务文件中。我变得不确定。
这是我的代码
service.ts
getBillingCycles() {
return this.http.get('../../assets/download_1.json');
};
app.component.ts
export class AppComponent implements OnInit{
title = 'ngMTN';
billing: any;
constructor(private http: HttpClient, private BillingCyclesService: BillingCyclesServiceService) { }
ngOnInit() {
this.getBillingCycles();
}
getBillingCycles() {
this.BillingCyclesService.getBillingCycles()
.subscribe(data => this.billing = data);
console.log(this.billing);
}
}
答案 0 :(得分:5)
您需要将控制台日志放置在订阅中,
this.BillingCyclesService.getBillingCycles()
.subscribe((data) => {
this.billing = data;
console.log(this.billing);
});