<
script
setup
>
import
{dialogProps, dialogEmits, useDialog}
from
'../../composables/dialog'
const
props =
defineProps
({
...dialogProps,
const
emits =
defineEmits
([
...dialogEmits,
const
{visibleModel} =
useDialog
(props, emits)
</
script
>
一个最简单的组合就完成了.
useForm
1. 定义Form组合
import {ref} from 'vue'
export {bbForm as Form} from '../components/bb-form'
export {Input} from '@arco-design/web-vue'
export {InputNumber} from '@arco-design/web-vue'
export function useForm (key) {
const ref_form = ref(null)
return {
ref_form
2. 使用Form组合
<template>
<bb.Form ref="ref_form" :model="form">
</bb.Form>
</template>
<script setup>
import * as bb from '../../composables/form'
const form = ref({
name: ''
const {ref_form} = bb.useForm()
</script>
useFormInDialog
如果有这样一个需求, 弹框里加上表单,需要提交一些信息,需要先表单验证再提交.失败不关闭表单,成功后关闭表单.
就可以把上面定义的组合再组合一下.
1.定义useFormInDialog组合
import {ref} from 'vue'
import {Message} from '@arco-design/web-vue'
import {getErrorMessageContent} from '../utils/utils'
export function useFormInDialog (promise, visibleModel, ref_form = null) {
const okLoading = ref(false)
function beforeOk (done) {
let validatePromise = ref_form ? ref_form.value.bbValidate() : Promise.resolve()
validatePromise
.then(() => {
okLoading.value = true
promise()
.then(() => {
okLoading.value = false
Message.success('操作成功!')
done()
visibleModel ? visibleModel.value = false : ''
.catch(err => {
okLoading.value = false
if (Array.isArray(err) && err[0].name === 'ValidateError') {
} else {
Message.error(getErrorMessageContent(err))
.catch(err => {
console.warn('表单校验失败!', err)
okLoading.value = false
return false
return {
okLoading,
beforeOk
2.使用useFormInDialog组合
<template>
<a-modal
v-model:visible="visibleModel" // Dialog 组合
:ok-loading="okLoading" // FormInDialog 组合
@before-ok="beforeOk" // FormInDialog 组合
<template #title>查看和编辑</template>
<bb.Form ref="ref_form" :model="form"> // Form 组合
// 具体表单组件
</bb.Form>
</a-modal>
</template>
<script setup>
import axios from 'axios'
import {inject, ref, reactive} from 'vue'
import * as bb from '../../composables/form'
import {dialogProps, dialogEmits, useDialog} from '../../composables/dialog'
import {useFormInDialog} from '../../composables/form-dialog'
const props = defineProps({
...dialogProps,
uuid: {
type: String,
required: true
const emits = defineEmits([
...dialogEmits,
'edit-success'
const {visibleModel} = useDialog(props, emits)
const form = ref({
const rules = reactive({
const {ref_form} = bb.useForm()
const {okLoading, beforeOk} = useFormInDialog(() => {
return new Promise(async (resolve, reject) => {
try {
await axios.put(`具体api`, {...form.value})
resolve()
emits('edit-success')
} catch (err) {
reject(err)
}, visibleModel, ref_form)
</script>
组合之后的代码比我之前写vue2的时候更紧凑了. 在如何写业务代码的时候更多了一些思考.