Jest.js Angular服务将方法解释为属性?

yfjy0ee7  于 7个月前  发布在  Jest
关注(0)|答案(1)|浏览(60)

我已经为我的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的属性。相反,它是一个方法,我不知道为什么编译器不能识别它。

zvokhttg

zvokhttg1#

错误消息是非常明确的,你不能从外部调用私有方法:使其公开

public getDays(): string[] {
    return this.days.slice();
  }

字符串

相关问题