Photo Viewer
通过 Ionic Modal,从按钮或现有页面方法中调用PhotoViewerPage组件。请在安装步骤之后执行此操作。在使用查看器时,需要安装swiper组件。
import { Component, inject } from '@angular/core';
import { ModalController, IonButton } from '@ionic/angular';
import { PhotoViewerProps, PhotoViewerResult } from '@rdlabo/ionic-angular-photo-editor';
import { PhotoViewerPage } from '@rdlabo/ionic-angular-photo-editor/viewer';
@Component({
selector: 'app-view-photos',
imports: [IonButton],
template: `<ion-button type="button" (click)="openViewer()">View photos</ion-button>`,
})
export class ViewPhotosPage {
private readonly modalCtrl = inject(ModalController);
async openViewer(): Promise<void> {
const componentProps = {
imageUrls: ['https://picsum.photos/200/300', 'https://picsum.photos/200/301'],
index: 0,
isCircle: false,
enableDelete: true,
toolbarColorScheme: 'dark',
imageAlt: (url, index) => `Photo ${index + 1}`,
labels: {
delete: 'Delete',
},
} satisfies PhotoViewerProps;
const modal = await this.modalCtrl.create({
component: PhotoViewerPage,
componentProps,
});
await modal.present();
const { data } = await modal.onWillDismiss<PhotoViewerResult>();
if (data?.action === 'delete') {
console.log(data.index, data.value);
}
}
}
模态框返回的结果
用户点击删除时,模态框关闭并返回以下数据:
interface PhotoViewerResult {
action: 'delete';
index: number;
value: string; // 指定索引处的图片 URL
}
直接关闭或向下滑动时,模态框不返回数据。
选项
imageUrls: string[]
必填项。 需要显示的图片 URL 或数据 URL。
index: number
初始幻灯片索引。默认值为0。
isCircle: boolean
当值为true时,图片会以圆形方式显示。
enableDelete: boolean
当值为true时,会显示删除按钮。
enableFooterSafeArea: boolean
当设置为true时,会在 iOS 设备上为页脚添加安全区域填充效果。
toolbarColorScheme: 'light' | 'dark'
必填项。 使用 dark 样式来设置深色/黑色的 ion-toolbar 组件,而使用 light 样式来设置浅色/白色的工具栏组件。详情请参阅主题说明。
imageAlt: string | ((url: string, index: number) => string)
每个幻灯片图片都应具有易于理解的alt文本描述。默认情况下,alt 文本为空字符串。如果 alt 文本需要依赖于 URL 或索引信息,可以传递一个回调函数来处理这种情况。
labels: Partial<PhotoViewerLabels>
可以覆盖默认的 UI 字符串设置。如果未指定某个键的值,则仍然使用内置的日语默认值。
| 键 | 默认值(日语) |
|---|---|
| close | 閉じる |
| delete | 削除 |
关闭按钮也使用了aria-label属性,该属性来自close标签。