从角度服务和控制台获取数据记录它

sky*_*dev 3 typescript angular

我正在尝试将app.component文件中的console.log数据作为我的服务文件.我没有定义.

这是我的代码

service.ts

getBillingCycles() {
    return this.http.get('../../assets/download_1.json');
};
Run Code Online (Sandbox Code Playgroud)

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

Saj*_*ran 5

您需要将控制台日志放在订阅中,

this.BillingCyclesService.getBillingCycles()
        .subscribe((data) => {
         this.billing = data;
         console.log(this.billing);
});
Run Code Online (Sandbox Code Playgroud)