简体   繁体   中英

Angular Calling function in service from component

This is my service,

@Injectable({   providedIn: 'root' }) 
export class DashboardService {
    BacklogList: EngBacklog;   
    constructor(private http: HttpClient) { 
    }

    getEngBacklog(){
        return this.http.get(environment.apiURL + 'PSP');
    } 
}

And this is my component,

export class DashboardComponent implements OnInit {

radioModel: string = 'Month';
EngBacklogList: EngBacklog[];

constructor(private dashboardService: DashboardService){} 
ngOnInit(): void {
     this.dashboardService.getEngBacklog().subscribe(
       res => this.EngBacklogList = res as EngBacklog[]);  
}

It gives an error saying : Property 'getEngBacklog' does not exist on type 'DashboardService'

What can be the problem?

Your code seems to be fine for me, but there maybe two reasons for your issue,

  1. Proper import statements

Check if your import statement for your Service class has done properly.

  1. Dependency Injection

You did not gave your Service class in Providers array in your Module.

Check here more about Service class usage .

Not seeing any issues in the code. 'EngBacklog' type already exists. Your method name says 'getEngBacklog()'. Try changing the method name and check whether it helps.

Service:

getEngBacklogData(){
    return this.http.get(environment.apiURL + 'PSP');
} 

Component:

this.dashboardService.getEngBacklogData().subscribe(
    res: EngBacklog[] => this.EngBacklogList = res);  
}

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