类型ionic 4和Angular不存在属性'onDidDismiss'

时间:2019-05-08 23:54:38

标签: angular typescript ionic-framework ionic4

我正在开发应用程序,并且在TypeScript中有下一堂课。

当它消除模态时,我想尝试通过“ ModalController”中的类型回调方法获取数据:

import { Component, OnInit } from '@angular/core'; 
import {NavController, ModalController} from '@ionic/angular'; 
import { Router } from '@angular/router'; 
import { HomePage } from '../home/home.page'; 
import {AddItemPage} from '../add-item/add-item.page';

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

export class TodoComponent implements OnInit {

  public items;

  constructor(public navCtrl: NavController, public modalCtrl: ModalController) {
    this.ionViewDidLoad();    
  }

  ngOnInit() {}

  async addItem()  {
    // Create a modal using MyModalComponent with some initial data 
     const modal = await this.modalCtrl.create({   
        component: AddItemPage,  componentProps: {
           'prop1': "cadena!!!!!!!"   
        } 
     }).then(function(modal) {   
        return modal.present(); 
     });

     modal.onDidDismiss(() => {
        // Call the method to do whatever in your home.ts
        console.log('Modal closed');
    });
  }
}

我收到此错误:

  

core.js:15724错误错误:未捕获(承诺):TypeError:无法   读取undefinedTypeError的属性'onDidDismiss':无法读取   属性'onDidDismiss'的定义

3 个答案:

答案 0 :(得分:0)

由于您使用的是await来创建模态,因此不需要使用then

请尝试以下操作:

async addItem()  {

  // Create the modal
  const modal = await this.modalCtrl.create({   
    component: AddItemPage,
    componentProps: {
      'prop1': "cadena!!!!!!!"   
    } 
  });

  // Present the modal
  return await modal.present();

  // Wait for the modal to be dismissed before continuing...
  const eventDetails = await modal.onDidDismiss();

  if (eventDetails) {
    console.log(eventDetails.data);
  }

  // ...

}

上面的代码假定您在退出模态时正在返回某些内容,如下所示:

// Dismiss the modal returning some data object
modalController.dismiss({
  'result': 'This is the result returned by the modal!'
});

答案 1 :(得分:0)

尝试:

const modal = await modalController.create({...});
const { data } = await modal.onDidDismiss();
console.log(data);

根据官方documentation

答案 2 :(得分:0)

这篇文章的相关性:https://medium.com/@david.dalbusco/how-to-declare-and-use-modals-in-ionic-v4-4d3f42ac30a3 我的错误只是我如何创建模态。我已经按照以下代码创建了模态属性。

class CustomImageNamePipeline(ImagesPipeline): #I copied this code from the website

    def get_media_requests(self, item, info):
        return [Request(x, meta={'image_name': item["image_name"]})
                for x in item.get('image_urls', [])]

    def file_path(self, request, response=None, info=None):
        return '%s.jpg' % request.meta['image_name']