来自模型的测试变量

Test variable from models

我正在使用 IONIC 4,我遇到了一个听起来很愚蠢的情况,但我坚持了下来。

我有一个这样的模型class:

export class Media {
  public id: number;
  public name: string;
  public description: string;
  public episode: number;
  public startDate: string;
  public endDate: string;
  public slates: any[];

  constructor() {
    this.id = 0;
    this.name = "";
    this.description = "";
    this.startDate = "";
    this.endDate = "";
    this.episode = 1;
    this.slates = [];
  }
}

我在单击按钮时打开一个模式来插入此媒体:

  async goToNewMediaForm() {
    const newMediaModal = await this.modalCtrl.create({
      component: MediaNewPage
    });

    newMediaModal.present();
  }

它打开了媒体插入页面,基于我在下面发布的Class,一切正常。

现在,我想在用户退出此模式时做一些事情,所以我正在使用这样的东西:

async goToNewMediaForm() {
    const newMediaModal = await this.modalCtrl.create({
      component: MediaNewPage
    });

    newMediaModal.onDidDismiss().then(data => {
        this.navCtrl.navigateForward("slate-list");
    });
    newMediaModal.present();
  }

所以,我所做的是将用户移动到另一个页面。但是我必须有一种方法来区分用户是单击保存还是只是单击退出模式,从而关闭它。我只希望用户在单击保存时被转发到下一页,而不是取消模式。

当我console.log数据参数时,我有这个:

{data: Media, role: undefined}
data: Media
id: 23
name: "eeeee"
description: ""
startDate: ""
endDate: ""
episode: 1
slates: []
__proto__: Object
role: undefined
__proto__: Object

当用户点击模态框上的取消按钮时:

{data: undefined, role: undefined}
data: undefined
role: undefined
__proto__: Object

并且当用户在模式之外单击时:

{data: undefined, role: "backdrop"}
data: undefined
role: "backdrop"
__proto__: Object

我尝试测试数据是否为媒体类型,如果是媒体实例,则没有成功..

      console.log(data instanceof Media); //False
      console.log(typeof data); //object
      console.log(data === undefined); //False
      console.log(data === "undefined"); //False
      console.log(data == undefined); //False
      console.log(data == "undefined"); //False

我错过了什么?如果这个 "data" 变量是一个对象,即使用户点击退出模式或点击取消按钮,我该如何测试它?

需要先关闭模态框,然后才能在应用程序中继续操作。这在文档中有说明:

A Modal is a dialog that appears on top of the app's content, and must be dismissed by the app before interaction can resume.

ion-modal documentation


I just want the user to be forwarded to the next page if he clicked save, not cancelled the modal.

您可以指定一个自定义角色来关闭 Modal 或指定数据来实现相同的目的。请参阅下面的示例,将 save 角色传递给您的页面并触发导航。例如,当您通过在模态页面外部单击来关闭模态时,角色设置为 backdrop.

模态

this.modalController.dismiss(undefined, 'save');

页数

newMediaModal.onDidDismiss().then((data: object) => {
  if (data['role'] === 'save') this.navCtrl.navigateForward("slate-list");
});