0

Есть класс с такой функцией.

@Injectable()
export class PlatformFormService {
 public getTaskCandidateGroupId(): string {
      const task: any = this.platformStateService.getSelectedTask();
      if (task && task._embedded && task._embedded.identityLink && task._embedded.identityLink.length > 0) {
        return task._embedded.identityLink[0].groupId;
      }
      return undefined;
  }
}

в разных классах функция работает корректно. Но вот тут ее вызовы валиться с ошибкой.

export function validateDocuments(platformService: PlatformFormService): ValidationErrors | null {

  const platformFormService: any =  PlatformFormService;
  const candidateGroupId: string = platformFormService.getTaskCandidateGroupId();
}

как мне функцию getTaskCandidateGroupId() ее вызвать?

если вызываю через переменную в методе вот так

const candidateGroupId: string = platformService.getTaskCandidateGroupId();

то падает ошибка ERROR TypeError: Cannot read property 'getTaskCandidateGroupId' of undefined

2
  • 1
    Попробуйте const platformFormService: any = new PlatformFormService(); И это у вас не js, а скорее всего ts. Angular? 20 фев 2020 в 9:28
  • да это ts, и как вы сказали сработало 22 фев 2020 в 14:30

1 ответ 1

0

В данном методе лишняя строчка

const platformFormService: any =  PlatformFormService;

В ней в локальную переменную сохраняется сам класс. Так как метод не статичный, при попытке вызвать его непосредственно у класса - приводит к ошибке.

Вместо этого нужно воспользоваться переданным параметром. Для этого достаточно удалить указанную строку.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.