Случайно возникает ошибка инициализации компонента в Nativescript - PullRequest
0 голосов
/ 29 марта 2019

Простая страница компонента загружается после процесса входа с помощью навигации (RouterExtensions). Этот компонент показывает только случайную фразу из массива.

Иногда, не всегда, только примерно в 20% случаев, происходит сбой компонента с ошибкой фразы свойства undefined. Ошибка в эмуляторе и в устройстве.

Это код компонента:

HTML

<StackLayout class="stack-layout-1" horizontalAlignment="center" verticalAlignment="center" backgroundColor="#292731">
<GridLayout rows="*, auto, auto, auto">
    <Image row="0" src="~/images/bulb.png" class="slide-image"></Image>
    <Label row="1" [text]="phrase.phrase" class="h4 m-x-20 primary-text" textWrap="true"></Label>
    <Label row="2" [text]="phrase.author" class="text-right m-r-30 secondary-text"></Label>
    <Button row="3" text="Continuar" (tap)="onTapContinue()"  class="btn btn-primary btn-rounded-sm" height="50"></Button>
</GridLayout>

TS

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

import { RouterExtensions } from 'nativescript-angular/router';
import { Page } from 'tns-core-modules/ui/page';

import { Phrase } from '../../models/phrase.model';
import { HelpersService } from './../../services/helpers.service';
import { ConfigService } from './../../services/config.service';

@Component({
  selector: 'ns-phrases',
  templateUrl: './phrases.component.html',
  styleUrls: ['./phrases.component.css'],
  moduleId: module.id,
})
export class PhrasesComponent implements OnInit {

  public phrase: Phrase = new Phrase();

  constructor(private routerExtensions: RouterExtensions,
              private page: Page,
              private configService: ConfigService,
              private helpersService: HelpersService) { }

  ngOnInit() {
    this.page.actionBarHidden = true;

    // Get random phrase
    this.phrase = this.configService.PHRASES[this.helpersService.getRandomNumber(1, 6)];
  }

  public async onTapContinue() {
    await this.routerExtensions.navigate(['/home'], {
      clearHistory: true,
      transition: {
        name: 'slideTop',
        duration: 350,
        curve: 'ease'
      }
    });
  }
}

ФРАЗОВЫЙ КЛАСС

export class Phrase {
  phraseId: number;
  phrase: string;
  author: string;

  constructor() {
    this.phraseId = 0;
    this.phrase = 'Vaya, esto es un poco embarazoso... No he podido cargar la frase del día.';
    this.author = 'Tu App móvil';
  }
}

Фраза инициализируется с некоторыми значениями по умолчанию. Ошибка:

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

Есть идеи о том, почему это случается несколько раз? С уважением Хосе

...