у меня такая ошибка: ошибка TS2339: свойство не существует для типа

#angular

#angular

Вопрос:

У меня такая ошибка: ошибка TS2339: свойство ‘getConfig’ не существует для типа ‘ConfigComponent’ это мой файл config.component.ts :

 import { Component, OnInit } from '@angular/core';

import { ConfigService } from '../config.service';

@Component({
  selector: 'app-config',
  templateUrl: './config.component.html',
  styleUrls: ['./config.component.css']
})

export class ConfigComponent implements OnInit {

  constructor(private configService : ConfigService) { }

  ngOnInit() {
    this.getConfig();
  }

  showConfig() {
    this.configService.getConfig()
      .subscribe((data: any) => this.config = {
          heroesUrl: data['heroesUrl'],
          textfile:  data['textfile']
      });
  }
}
  

это мой файл config.service.ts

 import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ConfigService {
  configUrl = 'assets/config.json';
  constructor(private http: HttpClient) { }

  getConfig() {
    return this.http.get(this.configUrl);
  }

}
  

И это мой файл config.json

 {
  "heroesUrl": "http://localhost:3000/Entrees",
  "textfile": "assets/textfile.txt"
}
  

Я не понимаю, почему у меня эта ошибка. В CMD у меня такая ошибка :

ОШИБКА в src / app /config /config.component.ts (16,10): ошибка TS2339: свойство ‘getConfig’ не существует для типа ‘ConfigComponent’. src / app /config/config.component.ts(21,38): ошибка TS2339: свойство ‘config’ не существует для типа ‘ConfigComponent’.

Комментарии:

1. getConfig метод доступен в вашей службе, а не в компоненте. Также в вашем компоненте нет свойства config . Получите базовое представление от angular.io

Ответ №1:

В ConfigComponent вы должны изменить свой метод на showConfig ,

 ngOnInit() {
    this.showConfig();
}
  

Ответ №2:

Думаю, вы пропустили объявление config переменной в классе и вызов showConfig вместо getConfig в ConfigComponent

 constructor(private configService : ConfigService, private config: <Config>) { }

ngOnInit() {
    this.showConfig();
}