Skip to content

nax-dialog

当前版本:0.2.3

居中对话框(确认 / 告警)。

  1. 声明式:页面里写 <nax-dialog v-model:show>,可插槽自定义内容
  2. 命令式:全局挂一次宿主后,业务只调 naxDialog() / naxDialogAlert() / naxDialogConfirm() 自定义任意复杂弹层请直接用 nax-picker

安装

easycom 自动生效,页面直接使用 <nax-dialog /> 即可。

建议同时安装主题包 uni_modules/nax-ui-theme 并在 App.uvue 引入主题变量,详见 主题接入

代码示例

声明式用法
uvue
<nax-button label="打开" @click="show = true"></nax-button>
<nax-dialog
  v-model:show="show"
  title="提示"
  content="确定执行该操作吗?"
  @confirm="onConfirm"
  @cancel="onCancel"
></nax-dialog>
uts
const show = ref(false)

function onConfirm() {
  // 点确定
}

function onCancel() {
  // 点取消或遮罩关闭(需 maskClosable)
}

自定义内容

uvue
<nax-dialog v-model:show="show" title="协议" :show-cancel="true" confirm-text="同意">
  <view class="custom">
    <text>这里可以放自定义布局</text>
  </view>
</nax-dialog>

更复杂的居中/底部弹层请用 nax-picker

命令式用法

1. 全局挂一次宿主

html
<nax-dialog />

与 toast 相同:挂在常驻根布局。
未挂宿主时回退 uni.showModal

2. 业务只调方法

uts
import {
  naxDialog as showNaxDialog,
  naxDialogAlert,
  naxDialogConfirm,
  hideNaxDialog
} from '@/uni_modules/nax-dialog/index.uts'

// 确认框(默认双按钮)
naxDialogConfirm({
  title: '删除',
  content: '删除后不可恢复',
  confirmType: 'error'
}).then((res) => {
  if (res.getBoolean('confirm') == true) {
    // 用户点了确定
  }
})

naxDialogConfirm({ title: '提示', content: '继续?' }).then((res) => {
  const action = res.getString('action') // confirm | cancel
})

// 告警(仅确定)
naxDialogAlert('保存成功')
naxDialogAlert({ title: '提示', content: '网络已恢复' })

// 通用入口
showNaxDialog({
  title: '提示',
  content: '自定义双按钮',
  showCancel: true
})

微信小程序端:若当前 SFC 同时挂载 <nax-dialog /> 并导入 naxDialog,请像上例一样给函数设置本地别名。naxDialog 会与组件标签映射到同一个驼峰名,导致 MP-WEIXIN 未注册宿主组件;声明式与命令式按钮都会表现为点击无反应。仅调用函数、不挂载宿主的业务页可继续直接导入 naxDialog

asyncClose(异步关闭)

uts
naxDialogConfirm({
  title: '提交',
  content: '确认提交?',
  asyncClose: true
}).then((res) => {
  if (res.getBoolean('confirm') == true) {
    // 请求中…结束后再关
    // hideNaxDialog()
  }
})
命令式 API
方法说明
naxDialog(input?)通用打开,默认双按钮
naxDialogAlert(input?)告警,默认仅确定
naxDialogConfirm(input?)确认,默认取消+确定
hideNaxDialog() / closeNaxDialog()关闭当前命令式对话框

input 可为 string(content)或 object:

字段类型默认说明
titlestring''标题文案(显示在内容上方,加粗)
contentstring''正文文案;传 string 时等价于只传 content
messagestring''兼容字段,同 content
showCancelbooleantrue是否显示取消按钮
showConfirmbooleantrue是否显示确认按钮
cancelTextstring'取消'取消按钮文案
confirmTextstring'确认'确认按钮文案
confirmTypestring'primary'确认按钮色:primary 主要 / info 信息 / success 成功 / warning 警告 / error 错误(dangererror)/ default 默认
confirmButtonTypestring'primary'兼容字段,同 confirmType
maskClosablebooleanfalse点遮罩是否关闭弹层
closeOnClickOverlaybooleanfalse兼容字段,同 maskClosable
asyncClosebooleanfalsetrue 时点击确认不自动关闭,由业务手动关闭(异步提交场景)
widthstring''对话框宽度;纯数字按 px

返回 Promise<UTSJSONObject>,字段同上。

声明式 · 基础确认
uvue
<nax-button type="primary" label="打开确认框" @click="basicShow = true"></nax-button>

<nax-dialog
	v-model:show="basicShow"
	title="提示"
	content="确定执行该操作吗?"
	@confirm="onConfirm"
	@cancel="onCancel"
></nax-dialog>
uts
const basicShow = ref(false)

function onConfirm() {
	// 点击确定
}

function onCancel() {
	// 点击取消
}
声明式 · 告警(仅确定)

show-cancel 隐藏取消按钮,confirm-text 自定义确定文案。

uvue
<nax-button label="打开告警" @click="alertShow = true"></nax-button>

<nax-dialog
	v-model:show="alertShow"
	title="提示"
	content="操作已完成"
	:show-cancel="false"
	confirm-text="我知道了"
	@confirm="onConfirm"
></nax-dialog>
uts
const alertShow = ref(false)

function onConfirm() {
	// 点击确定
}
声明式 · 危险操作

confirm-type 指定确定按钮语义色。

uvue
<nax-button type="error" label="删除确认" @click="dangerShow = true"></nax-button>

<nax-dialog
	v-model:show="dangerShow"
	title="删除确认"
	content="删除后不可恢复,是否继续?"
	confirm-text="删除"
	confirm-type="error"
	@confirm="onConfirm"
	@cancel="onCancel"
></nax-dialog>
uts
const dangerShow = ref(false)

function onConfirm() {
	// 点击确定
}

function onCancel() {
	// 点击取消
}
声明式 · 自定义内容插槽

默认插槽自定义内容区。

uvue
<nax-button type="info" label="自定义内容" @click="customShow = true"></nax-button>

<nax-dialog
	v-model:show="customShow"
	title="用户协议"
	confirm-text="同意"
	cancel-text="拒绝"
	@confirm="onConfirm"
	@cancel="onCancel"
>
	<text class="dialog-content" :lines="6">请阅读并同意服务条款与隐私政策。本示例用默认插槽自定义内容区。</text>
</nax-dialog>
uts
const customShow = ref(false)

function onConfirm() {
	// 点击确定
}

function onCancel() {
	// 点击取消
}
声明式 · 异步关闭

async-close 开启后点确定不会自动关闭,需业务在请求结束后手动关 show。

uvue
<nax-button label="asyncClose" @click="asyncShow = true"></nax-button>
<nax-button size="sm" label="手动关闭" @click="asyncShow = false"></nax-button>

<nax-dialog
	v-model:show="asyncShow"
	title="提交"
	content="确认提交当前内容?"
	:async-close="true"
	@confirm="onAsyncConfirm"
	@cancel="onCancel"
></nax-dialog>
uts
const asyncShow = ref(false)

// 模拟异步请求:结束后手动关闭
function onAsyncConfirm() {
	setTimeout(() => {
		asyncShow.value = false
	}, 1500)
}

function onCancel() {
	// 点击取消
}
命令式 · API

宿主与 toast 相同,全局挂一次即可;未挂载时回退系统 showModal。

uvue
<nax-button size="sm" type="primary" label="naxDialogConfirm" @click="onApiConfirm"></nax-button>
<nax-button size="sm" label="naxDialogAlert" @click="onApiAlert"></nax-button>
<nax-button size="sm" type="error" label="危险确认" @click="onApiDanger"></nax-button>
<nax-button size="sm" label="Promise" @click="onApiPromise"></nax-button>

<!-- 命令式宿主:须放在声明式实例之后 -->
<nax-dialog></nax-dialog>
uts
import {
	naxDialog as showNaxDialog,
	naxDialogAlert,
	naxDialogConfirm
} from '@/uni_modules/nax-dialog/index.uts'

function onApiConfirm() {
	naxDialogConfirm({
		title: '确认',
		content: '使用命令式打开确认框'
	} as UTSJSONObject).then((res: UTSJSONObject) => {
		if (res.getBoolean('confirm') == true) {
			// 已确定
		}
	})
}

function onApiAlert() {
	naxDialogAlert({
		title: '提示',
		content: '这是一条告警信息'
	} as UTSJSONObject).then((res: UTSJSONObject) => {
		const action = res.getString('action')
		// action:'confirm' | 'cancel'
	})
}

function onApiDanger() {
	naxDialogConfirm({
		title: '删除',
		content: '删除后不可恢复',
		confirmText: '删除',
		confirmType: 'error'
	} as UTSJSONObject).then((res: UTSJSONObject) => {
		if (res.getBoolean('confirm') == true) {
			// 已删除
		}
	})
}

function onApiPromise() {
	showNaxDialog({
		title: 'Promise',
		content: '等待你的选择',
		showCancel: true
	} as UTSJSONObject).then((res: UTSJSONObject) => {
		const action = res.getString('action')
		// action:'confirm' | 'cancel'
	})
}

主题

通过 CSS 变量覆盖:

Token用途
--nax-color-bg背景色
--nax-color-bg-hover按压/悬停背景色
--nax-color-border边框色
--nax-color-error错误色
--nax-color-info信息色
--nax-color-primary主题主色
--nax-color-success成功色
--nax-color-text主文字色
--nax-color-text-secondary次要文字色
--nax-color-warning警告色

Props

属性类型默认值说明
showbooleanfalsev-model:show 显隐(声明式)
titlestring''标题
contentstring''内容文案
messagestring''同 content(兼容)
showCancelbooleantrue显示取消,默认 true
showConfirmbooleantrue显示确定,默认 true
cancelTextstring'取消'取消文案
confirmTextstring'确定'确定文案
confirmTypestring'primary'primary 主要 | info 信息 | success 成功 | warning 警告 | error 错误 | default 默认;默认 primary
asyncClosebooleanfalse点确定不自动关闭,业务自行关
maskbooleantrue遮罩,默认 true
maskClosablebooleanfalse点遮罩关闭,默认 false
closeOnMaskbooleanfalse同 maskClosable
closeOnClickOverlaybooleanfalse同 maskClosable
roundbooleantrue圆角,默认 true
widthstring''面板宽度
zIndexnumber10085层级,默认 10085
durationnumber280动画 ms,默认 280
customClassstring''根扩展 class

Events

事件说明
update:show显隐变更
confirm点确定
cancel点取消 / 遮罩关闭(声明式)
open打开开始
opened打开完成
close关闭完成
click-mask点遮罩

Slots

插槽说明
default自定义内容区
title自定义标题
footer自定义底部

与 nax-picker / toast 的关系

组件职责
nax-picker通用弹出容器(任意内容 / 四向)
nax-dialog标准确认/告警(组合 picker + 可选命令式)
nax-toast短反馈提示(命令式)
nax-action-sheet底部操作列表