ionic 4 ERROR TypeError: Невозможно прочитать свойство 'ready' из неопределенного - PullRequest
0 голосов
/ 06 июня 2019

Будучи хорошим программистом, я пытаюсь быть, я установил линтер и хаски. Когда я просматривал линтеры, я пришел к файлу app.component.ts, и все было красным. Не понравилось, как были объявления внутри конструктора. Хорошо, нет проблем, я изменил:

  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
  ) {
    this.initializeApp();
    }

до

  private platform: Platform;
  private splashScreen: SplashScreen;
  private statusBar: StatusBar;

  public constructor(
    platform: Platform,
    splashScreen: SplashScreen,
    statusBar: StatusBar,
    ){
     this.initializeApp();
     this.platform = platform;
     this.splashScreen = splashScreen;
     this.statusBar = statusBar;
    }

однако, после изменения я получил все типы ошибок:

ОШИБКА TypeError: Невозможно прочитать свойство 'ready' из неопределенного

это относится к:

  public initializeApp(): void {
    this.platform.ready().then( // <----ERROR
      (): void => {
        this.statusBar.styleDefault();
        this.splashScreen.hide();
      },
    );
  }

и тогда я также получу ошибку о типе и присвоении any[]

Главный вопрос, который у меня есть, почему? Почему объявление конструктора частного экземпляра в порядке, но затем объявление его частным образом за пределами конструктора, все еще передавая его через конструктор this.platform, не найдено. Кроме того, как я могу получить те же результаты без жалоб от Линтера.

Ответы [ 2 ]

1 голос
/ 06 июня 2019
  private platform: Platform;
  private splashScreen: SplashScreen;
  private statusBar: StatusBar;

  public constructor(
    platform: Platform,
    splashScreen: SplashScreen,
    statusBar: StatusBar,
    ){
     this.initializeApp(); // <-- initialize app called here.
     this.platform = platform; // <-- platform assigned here.
     this.splashScreen = splashScreen;
     this.statusBar = statusBar;
    }

Поскольку initializeApp вызывается ДО присваивания this.platform, его исходное значение undefined. Просто переместите initializeApp после назначения:

     this.platform = platform; // <-- platform assigned here.
     this.splashScreen = splashScreen;
     this.statusBar = statusBar;
     this.initializeApp(); // <-- initialize app called here.
1 голос
/ 06 июня 2019

Вы должны позвонить this.platform = platform; до this.initializeApp();, в противном случае ваши личные свойства будут неопределенными.Ваш линтер должен быть в порядке со следующим кодом:

public constructor(
    platform: Platform,
    splashScreen: SplashScreen,
    statusBar: StatusBar,
    ){
     this.platform = platform;
     this.splashScreen = splashScreen;
     this.statusBar = statusBar;
     this.initializeApp();
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...