简体   繁体   中英

ngOnInit component error , this is not a function at component.ngOnInit

I want to call a function that has been implemented in the service class but I keep getting a this.ApiService.dailychart is not a function at myComponentname.ngOnInit , I have checked and I used the subscribe function well and implemented the OnInit in the class . What could I be doing wrong ?


import { Component, OnInit } from '@angular/core';
import {ApiService} from '../api.service';
import {Chart} from 'chart.js';

@Component({
  selector: 'app-transactions',
  templateUrl: './transactions.component.html',
  styleUrls: ['./transactions.component.scss']
})
export class TransactionsComponent implements OnInit {

  constructor(private ApiService : ApiService) { }

  ngOnInit() {
    this.ApiService.dailychart()
    .subscribe(data => {
      console.log(data);
    });
  }

}


Change the name to something different from the service class:

constructor(private apiService : ApiService) { }
ngOnInit() {
    this.apiService.dailychart()
    .subscribe(data => {
      console.log(data);
    });
  }

In my case, the solution was that on the API side, the endpoint was missing the word async :

API:

@Get('/')
async dailychart (){
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM