我已经为我的Angular组件LiveDashboardComponent
创建了一个Angular服务,使用一个方法来示例化组件的属性。
组件
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();
}
}
字符串
服务
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();
}
}
型
我遇到了一个问题,编译器在getDays方法 name 下面加了下划线,这意味着它不包括括号()
,在我的组件的方法实现中有以下消息。
Property 'getDays' is private and only accessible within class 'LiveDashboardService'
型
然而,正如代码片段所展示的,我在我的Angular服务中没有一个名为getDays
的属性。相反,它是一个方法,我不知道为什么编译器不能识别它。
1条答案
按热度按时间zvokhttg1#
错误消息是非常明确的,你不能从外部调用私有方法:使其公开
字符串