Skip to content

Actionsheet 操作菜单

从底部弹出的操作菜单,用于提供一组操作项供用户选择。支持声明式和命令式两种调用方式。

基础用法

通过 v-model:visible 控制显示,items 设置菜单项,@select 监听选择。

查看代码
vue
<template>
  <weui-button type="primary" @click="show = true">显示 Actionsheet</weui-button>
  <weui-actionsheet
    v-model:visible="show"
    :items="items"
    @select="onSelect"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue'
import type { ActionsheetItem } from 'weui-uniapp-design'

const show = ref(false)
const items: ActionsheetItem[] = [
  { label: '拍照' },
  { label: '从相册选择' },
]

const onSelect = (item: ActionsheetItem, index: number) => {
  console.log(item, index)
}
</script>

带标题

通过 title 设置标题。

查看代码
vue
<template>
  <weui-actionsheet
    v-model:visible="show"
    title="选择图片来源"
    :items="items"
  />
</template>

警告操作

通过 warn: true 将菜单项设为警告样式(红色文字),常用于删除等危险操作。

查看代码
vue
<template>
  <weui-actionsheet
    v-model:visible="show"
    :items="warnItems"
  />
</template>

<script setup lang="ts">
import type { ActionsheetItem } from 'weui-uniapp-design'
const warnItems: ActionsheetItem[] = [
  { label: '编辑' },
  { label: '删除', warn: true },
]
</script>

带提示文字

通过 tips 为菜单项添加说明文字。

查看代码
vue
<template>
  <weui-actionsheet
    v-model:visible="show"
    :items="tipsItems"
  />
</template>

<script setup lang="ts">
import type { ActionsheetItem } from 'weui-uniapp-design'
const tipsItems: ActionsheetItem[] = [
  { label: '复制', tips: '复制到剪贴板' },
  { label: '转发', tips: '转发给好友' },
]
</script>

禁用遮罩点击

通过 :mask-closable="false" 禁止点击遮罩关闭,必须选择菜单项或取消。

查看代码
vue
<template>
  <weui-actionsheet
    v-model:visible="show"
    :items="items"
    :mask-closable="false"
  />
</template>

自定义取消文字

通过 cancel-text 自定义取消按钮文字,设为空字符串可隐藏取消按钮。

查看代码
vue
<template>
  <weui-actionsheet
    v-model:visible="show"
    :items="items"
    cancel-text=""
  />
</template>

命令式调用

通过 Actionsheet.show(options) 命令式调用,返回 Promise。点击菜单项 resolve { item, index },点击取消/遮罩 resolve { item: null, index: -1 }

查看代码
vue
<template>
  <weui-overlay-host />
  <weui-button type="primary" @click="onImperative">Actionsheet.show</weui-button>
</template>

<script setup lang="ts">
import { Actionsheet, type ActionsheetItem } from 'weui-uniapp-design'

const items: ActionsheetItem[] = [
  { label: '拍照' },
  { label: '从相册选择' },
]

const onImperative = async () => {
  const result = await Actionsheet.show({ title: '命令式调用', items })
  if (result.index >= 0) {
    console.log('选中', result.item, result.index)
  }
}
</script>

Attributes

参数说明类型默认值
visible (v-model)是否显示booleanfalse
title标题string
items菜单项列表ActionsheetItem[][]
cancel-text取消按钮文字,为空时不显示操作区string取消
mask-closable点击遮罩是否关闭booleantrue
ext-class自定义附加类名string
z-indexz-index(命令式调用时由 overlay-host 注入)number

ActionsheetItem

属性说明类型默认值
label菜单项文字string
tips提示文字(显示在 label 下方)string
warn是否为警告样式(红色文字)booleanfalse

Events

事件名说明回调参数
select选择菜单项时触发(item: ActionsheetItem, index: number)
cancel点击取消按钮时触发
close关闭时触发

命令式 API

Actionsheet.show(options): Promise<ActionsheetShowResult>

参数说明类型默认值
title标题string
items菜单项列表ActionsheetItem[]
cancelText取消按钮文字string'取消'
maskClosable点击遮罩是否关闭booleantrue
extClass自定义附加类名string

返回 Promise,resolve 值:

字段说明类型
item被点击的菜单项,取消时为 nullActionsheetItem | null
index被点击的菜单项索引,-1 表示取消number