Issue
I have created an Angular service for my Angular component, LiveDashboardComponent, with a method to instantiate the component's property.
Component
import { Component, OnInit } from '@angular/core';
import { LiveDashboardService } from './live-dashboard.service';
@Component({
selector: 'c8y-live-dashboard',
templateUrl: 'live-dashboard.component.html',
styleUrls: ['live-dashboard.component.less'],
})
export class LiveDashboardComponent {
days: string[] | undefined;
constructor(private liveDashboardService: LiveDashboardService) {}
ngOnInit() {
this.getDays();
}
private getDays(): void {
this.days = this.liveDashboardService.getDays();
}
}
Service
import { Injectable } from "@angular/core";
@Injectable({
providedIn: "root",
})
export class LiveDashboardService {
private days: string[] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
constructor() {}
private getDays(): string[] {
return this.days.slice();
}
}
I am having a problem whereby the compiler underlines the getDays method name, meaning that it does not include the parenthesis (), in my component's implementation of the method with the following message.
Property 'getDays' is private and only accessible within class 'LiveDashboardService'
However, as the code snippet demonstrates, I do not have a property called getDays in my Angular service. Instead, it is a method, and I do not know why the compiler cannot identify it as such.
Solution
The error message is quite explicit, you can't call a private method from the outside : make it public
public getDays(): string[] {
return this.days.slice();
}
Answered By - Matthieu Riegler
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.