返回介绍

Ionic4 模态框组件 ion-modal

发布于 2019-11-22 18:04:17 字数 12227 浏览 1703 评论 0 收藏 0

Ionic4项目中我们可以使用Ionic4模态框组件ion-modal对项目进行布局。

ion-modal官方文档地址:https://ionicframework.com/docs/api/modal

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. It is useful as a select component when there are a lot of options to choose from, or when filtering items in a list, as well as many other use cases.

Creating

Modals can be created using a Modal Controller. They can be customized by passing modal options in the modal controller's create method.

Passing parameters

When a modal is created, parameters might be passed to the newly created modal:

// Create a modal using MyModalComponent with some initial data
const modal = await modalController.create({
  component: MyModalComponent,
  componentProps: {
    'prop1': value,
    'prop2': value2
  }
});

Under the hood, the controller creates a new ion-modal and attaches the specified component to it. It also assigns the specified componentProps to the component's instance:

// pseudo-code
const instance = create(MyModalComponent);
instance.prop1 = value;
instance.prop2 = value2;

This way, your component can access the passed params, check the "ion-modal 用法(Usage)" section for further code example for each frameworks.

Returning data

Modals can also return data back to the controller when they are dismissed.

const modal = await modalController.create({...});
const { data } = await modal.onDidDismiss();
console.log(data);
// Dismiss the top modal returning some data object
modalController.dismiss({
  'result': value
})

ion-modal 用法(Usage)

import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { ModalPage } from '../modal/modal.page';
@Component({
  selector: 'modal-example',
  templateUrl: 'modal-example.html',
  styleUrls: ['./modal-example.css']
})
export class ModalExample {
  constructor(public modalController: ModalController) {}

  async presentModal() {
    const modal = await this.modalController.create({
      component: ModalPage,
      componentProps: { value: 123 }
    });
    return await modal.present();
  }
}
import { Component, Input } from '@angular/core';
import { NavParams } from '@ionic/angular';

@Component({
  selector: 'modal-page',
})
export class ModalExample {

  // "value" passed in componentProps
  @Input() value: number;

  constructor(navParams: NavParams) {
    // componentProps can also be accessed at construction time using NavParams
  }

}

Lazy Loading

When lazy loading a modal, it's important to note that the modal will not be loaded when it is opened, but rather when the module that imports the modal's module is loaded.

For example, say there exists a CalendarComponent and an EventModal. The modal is presented by clicking a button in the CalendarComponent. In Angular, the EventModalModule would need to be included in the CalendarComponentModule since the modal is created in the CalendarComponent:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { IonicModule } from '@ionic/angular';

import { CalendarComponent } from './calendar.component';
import { EventModalModule } from '../modals/event/event.module';

@NgModule({
    declarations: [
        CalendarComponent
    ],
    imports: [
      IonicModule,
      CommonModule,
      EventModalModule
    ],
    exports: [
      CalendarComponent
    ]
})

export class CalendarComponentModule {}

  
</body>
customElements.define('modal-page', class extends HTMLElement {
  connectedCallback() {
    this.innerHTML = `

  
    Super Modal
  


  Content
`;
  }
});

async function presentModal() {
  // initialize controller
  const modalController = document.querySelector('ion-modal-controller');
  await modalController.componentOnReady();

  // present the modal
  const modalElement = await modalController.create({
    component: 'modal-page'
  });
  await modalElement.present();
}
import React, { Component } from 'react'
import { IonModal } from '@ionic/react';

type Props = {}
type State = {
  showModal: boolean
}

export class ModalExample extends Component {

  constructor(props: Props) {
    super(props);
    this.state = {
      showModal: false
    };
  }

  render() {
    return (
       this.setState(() => ({ showModal: false }))}
      >
        

This is modal content

this.setState(() => ({ showModal: false }))}> Close Modal ); } }

  >
    
      
        {{ title }}
      
    
    
      {{ content }}
    
  
export default { name: 'Modal', props: { title: { type: String, default: 'Super Modal' }, }, data() { return { content: 'Content', } }, } </script>

  
    
      Open Modal
    
  



import Modal from './modal.vue'

export default {
  methods: {
    openModal() {
      return this.$ionic.modalController
        .create({
          component: Modal,
          componentProps: {
            data: {
              content: 'New Content',
            },
            propsData: {
              title: 'New title',
            },
          },
        })
        .then(m => m.present())
    },
  },
}
</script>

ion-modal 属性(Properties)

animated

Description

If true, the modal will animate.

Attributeanimated
Typeboolean
Defaulttrue

backdropDismiss

Description

If true, the modal will be dismissed when the backdrop is clicked.

Attributebackdrop-dismiss
Typeboolean
Defaulttrue

component

Description

The component to display inside of the modal.

Attributecomponent
TypeFunction | HTMLElement | null | string

componentProps

Description

The data to pass to the modal component.

Typeundefined | { [key: string]: any; }

cssClass

Description

Additional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces.

Attributecss-class
Typestring | string[] | undefined

enterAnimation

Description

Animation to use when the modal is presented.

Type((Animation: Animation, baseEl: any, opts?: any) => Promise<Animation>) | undefined

keyboardClose

Description

If true, the keyboard will be automatically dismissed when the overlay is presented.

Attributekeyboard-close
Typeboolean
Defaulttrue

leaveAnimation

Description

Animation to use when the modal is dismissed.

Type((Animation: Animation, baseEl: any, opts?: any) => Promise<Animation>) | undefined

mode

Description

The mode determines which platform styles to use.

Attributemode
Type"ios" | "md"

showBackdrop

Description

If true, a backdrop will be displayed behind the modal.

Attributeshow-backdrop
Typeboolean
Defaulttrue

ion-modal 事件(Events)

NameDescription
ionModalDidDismissEmitted after the modal has dismissed.
ionModalDidPresentEmitted after the modal has presented.
ionModalWillDismissEmitted before the modal has dismissed.
ionModalWillPresentEmitted before the modal has presented.

ion-modal 内置方法(Methods)

dismiss

Description

Dismiss the modal overlay after it has been presented.

Signaturedismiss(data?: any, role?: string | undefined) => Promise<boolean>

onDidDismiss

Description

Returns a promise that resolves when the modal did dismiss.

SignatureonDidDismiss() => Promise<OverlayEventDetail<any>>

onWillDismiss

Description

Returns a promise that resolves when the modal will dismiss.

SignatureonWillDismiss() => Promise<OverlayEventDetail<any>>

present

Description

Present the modal overlay after it has been created.

Signaturepresent() => Promise<void>

ion-modal中的CSS 自定义属性

NameDescription
--backgroundBackground of the modal content
--border-colorBorder color of the modal content
--border-radiusBorder radius of the modal content
--border-styleBorder style of the modal content
--border-widthBorder width of the modal content
--heightHeight of the modal
--max-heightMaximum height of the modal
--max-widthMaximum width of the modal
--min-heightMinimum height of the modal
--min-widthMinimum width of the modal
--widthWidth of the modal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文