学习笔记有参考,有翻译,有自己的感悟
先看实例代码:
步骤:
1.在ts文件中引入组件:
import { ActionSheetController } from 'ionic-angular';
2.在construct构造函数中声明注入:
constructor(public navCtrl: NavController,public actionSheetCtrl: ActionSheetController) {}
3.编写Action Sheets组件的方法:
actionsheet() { let actionSheet =this.actionSheetCtrl.create({ title: '更换头像', cssClass:'head', buttons: [ { cssClass:'pai', text: '拍照', icon: 'ios-arrow-forward', role: 'destructive', handler: () => { console.log('Destructive clicked'); } },{ text: '从相册选择', icon: 'ios-arrow-forward', handler: () => { console.log('Archive clicked'); } },{ text: '取消', role: 'cancel', handler: () => { console.log('Cancel clicked'); }} ] }); actionSheet.present(); }
4.注:其中cssClass是关键,是定义的这个弹窗的class名,可以根据这个class名对弹窗进行样式的自定义,这个class名的使用一定要在app文件夹里面的app.scss里面进行修改,不然不起作用。buttons里面的icon是添加ionic自带的图标的
除了注意SCSS必须卸载app.scss里面才有用呢另外自己所需要的样式可以在firebar或者审查元素查看控制当前样式的类名,然后对类样式进行重新定义
app.scss文件代码:
.head{ .action-sheet-group{ .action-sheet-title{ padding: 1rem; font-size: 20px; color: #000; background-color:pink; } .disable-hover{ min-height: auto; font-size: 18px; color: #000; background: #fff; position: relative; .button-inner{ position: relative; .action-sheet-icon{ position: absolute; right: 4px; } } } .action-sheet-cancel{ background: #fefef9; .button-inner{ justify-content:center; font-weight: normal; } } } }
完整html:
<h2 (click)="actionsheet()">actionsheet</h2>
完整ts:
import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; import { IonicPage, ActionSheetController } from 'ionic-angular'; @IonicPage() @Component({ selector: 'page-home', templateUrl: 'home.html' }) export class HomePage { public simpleColumns: any[];//先定义一个任意类型的数组来接收参数 constructor(public navCtrl: NavController,public actionSheetCtrl:ActionSheetController) { this.simpleColumns = [ { name: 'col1',//列名,一个对象表示一列多选,一个multipicker有几列选项就有几个对象 options: [ //每一列的选项集 { text: '选项1', value: '选项1' }, { text: '选项2', value: '选项2' }, { text: '选项3', value: '选项3' } ] },{ name: 'col2', options: [ { text: '1-1', value: '1-1' }, { text: '1-2', value: '1-2' }, { text: '2-1', value: '2-1' }, { text: '2-2', value: '2-2' }, { text: '3-1', value: '3-1' } ] },{ name: 'col3', options: [ { text: '1-1-1', value: '1-1-1' }, { text: '1-1-2', value: '1-1-2' }, { text: '1-2-1', value: '1-2-1' }, { text: '1-2-2', value: '1-2-2' }, { text: '2-1-1', value: '2-1-1' }, { text: '2-1-2', value: '2-1-2' }, { text: '2-2-1', value: '2-2-1' }, { text: '2-2-2', value: '2-2-2' }, { text: '3-1-1', value: '3-1-1' }, { text: '3-1-2', value: '3-1-2' } ] } ]; } actionsheet() { let actionSheet =this.actionSheetCtrl.create({ title: '更换头像', cssClass:'head', buttons: [ { cssClass:'pai', text: '拍照', icon: 'ios-arrow-forward', role: 'destructive', handler: () => { console.log('Destructive clicked'); } },{ text: '从相册选择', icon: 'ios-arrow-forward', handler: () => { console.log('Archive clicked'); } },{ text: '取消', role: 'cancel', handler: () => { console.log('Cancel clicked'); }} ] }); actionSheet.present(); } }
完整知识翻译自官网同时加上自己的理解:
ActionSheet是一个对话框,让用户选择一个选项。而且用户必须要选择其中一个选项才能恢复与应用程序的交互(点击背景会执行 cancel
的事件)。当然也可以利用背景或者后退键来取消对话框从而恢复和程序的交互。
ActionSheet会从一个 button
数组创建它的按钮选项。每一个按钮都拥有一些属性,例如 text
handler
role
等等。如果 handler
返回 false
时,ActionSheet将不会消失。ActionSheet还可以选择有一个标题和副标题。
如果有一个button的 role
被设置为 cancel
那么这个按钮不管位于按钮数组的哪个位置它都会位于底部。ionic官方建议 destructive
类型的按钮最好位于数组的第一个位置。另外,如果ActionSheet是由于点击背景而被取消的,那么它将会执行和 cancle
类型的按钮点击一样的事件。
注意:如果有两个 cancle
类型的按钮,那么最后一个 cancel
类型的按钮会覆盖掉前面所有的 cancel
类型的按钮。
您可以在create方法的第一个参数中传递所有操作工作表的选项:ActionSheet.create(opts)。 否则,操作表的实例有添加选项的方法,如setTitle()或addButton()。
用法:
实例成员:
create(opts)
静态方法
create(opts)
属性名称 | 类型 | 描述 |
---|---|---|
title | string | ActionSheet的标题 |
subTitle | string | ActionSheet的副标题 |
cssClass | string | 自定义样式的css类 |
enableBackdropDismiss | boolean | 用户点击背景是否关闭ActionSheet |
buttons | array<any> | 显示的按钮的数组 |
按钮的属性
属性名称 | 类型 | 描述 |
---|---|---|
text | string | 按钮上显示的文字 |
icon | icon | 按钮上显示的图标 |
handler | any | 点击后执行的函数 |
cssClass | stirng | |
role | string | 如何显示按钮, destructive 或者 cancel 。如果没有设置此选项,那么将显示默认的按钮。 |
实例方法
setTitle(title)
属性名称 | 类型 | 描述 |
---|---|---|
title | string | 设置ActionSheet的标题 |
setSubTitle(subTitle)
属性名称 | 类型 | 描述 |
---|---|---|
subTitle | string | 设置ActionSheet的子标题 |
addButton(button)
属性名称 | 类型 | 描述 |
---|---|---|
button | object | ActionSheet的按钮。 |