【问题标题】:ionic 4 using modals via the modalcontrollerionic 4 通过模态控制器使用模态
【发布时间】:2019-01-17 13:58:44
【问题描述】:

我正在使用 Ionic 4,并希望通过 ModalController 使用模态。在 Ionic 3.x 中它相当简单,但我在 Ionic 4 中遇到了一些错误:

我尝试在其中启动 Modal 的页面:

import { Component, OnInit } from '@angular/core';
import { NavController, NavParams, ModalController } from '@ionic/angular';
import { MyModalPage } from '../MyModalPage/MyModalPage.page';

@Component({
  selector: 'app-product-display',
  templateUrl: './product-display.page.html',
  styleUrls: ['./product-display.page.scss'],
})
export class ProductDisplayPage implements OnInit {

  private params:any = {};
  public product:product = {};

  constructor(

    public modalCtrl : ModalController
  ) { 

  }//End of Constructor

  ngOnInit() {
  }

  async openModal()
  {

    var data = { message : 'hello world' };

    const modalPage = await this.modalCtrl.create({
      component: MyModalPage, 
      componentProps:{values: data}
    });

    return await modalPage.present();
  }

}

我的模态页面:

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

@Component({
  selector: 'app-my-modal',
  templateUrl: './my-modal.html',
  styleUrls: ['./my-modal.page.scss'],
})
export class MyModalPage implements OnInit {

  constructor(

  ) { }//End of Constructor

  ngOnInit() {
  }//End of ngOnInit

  closeModal()
  {


    //TODO: Implement Close Modal this.viewCtrl.dismiss();
  }

}//End of Class

我收到一条错误消息,指出它可能未包含在条目组件中:

ERROR 错误:未捕获(承诺中):错误:未找到组件工厂 对于 MyModalPage。你添加到 @NgModule.entryComponents?错误:找不到组件工厂 我的模态页面。你把它添加到@NgModule.entryComponents 了吗?

当我将它添加到产品显示模块的入口组件时,我收到另一个错误,指出它尚未导入。当我将它添加到产品显示模块的导入中时,我得到以下信息:

core.js:1671 ERROR 错误:未捕获(承诺中):错误:意外 模块导入的指令“MyModalPage” 'ProductDisplayPageModule'。请添加 @NgModule 注释。错误: 模块导入的意外指令“MyModalPage” 'ProductDisplayPageModule'。请添加@NgModule 注解。

如何使用模态控制器在 Ionic 4 / Angular 6 中呈现模态?

谢谢

【问题讨论】:

    标签: angular ionic-framework modal-dialog


    【解决方案1】:

    不幸的是,Ionic v4 Modals 不能延迟加载页面(但是,我希望以后可以这样做)。因此,您必须像以前一样导入组件,但是,您还需要将其添加到 app 模块中的声明中。像这样:

    @NgModule({
        declarations: [
            AppComponent,
            MyModalPage
        ],
        entryComponents: [
            MyModalPage
    ],
    ...
    

    希望这会有所帮助!我相信文档会在这一点上有所改进,因为我也遇到了这个问题。

    【讨论】:

    • 看来您也可以在您希望调用模式页面而不是应用程序级别的页面模块中执行此操作:@NgModule({ imports: [ CommonModule, FormsModule, IonicModule, RouterModule.forChild(routes)], declarations: [MyPage, MyModalPage], entryComponents: [MyModalPage] })
    • 如果MyModalPage 有依赖项,这对我不起作用(也就是需要加载模块,因为在这种情况下,我不仅需要将我的页面添加到声明和 entryComponents,还需要导入所有模块使用它的页面中的模态需求。:|
    • 应该entryComponents: [MyModalAddPage]entryComponents: [MyModalPage] 吗? Add 是不是偶然添加
    • 模态控制器的大问题 - 当用户点击手机后退按钮时,它会弹出模态控制器后面的页面。
    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2021-03-04
    • 2020-07-08
    • 2022-12-29
    • 2017-05-06
    相关资源
    最近更新 更多