父组件 parent.vue
<template>
<div class="item-wrap">
<!-- 模块内容 -->
<el-steps style="max-width: 600px" :active="active" finish-status="success" direction="enum" align-center>
<el-step v-for="item in tableData" :key="item" :title="item.total" :icon="item.icon"> </el-step>
</el-steps>
<div>
// 三个步骤组件
<Form1 v-show="active == 1" @nextSetp="nextSetp" />
<Form2 v-show="active == 2" @prevSetp="prevSetp" @nextSetp="nextSetp" />
<Form3 v-show="active == 3" @prevSetp="prevSetp" @submitSetp="submitSetp" />
</div>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
import { Edit, Picture, Upload } from '@element-plus/icons-vue'
import Form1 from './Form1.vue'
import Form2 from './Form2.vue'
import Form3 from './Form3.vue'
const tableData = ref([
{
total: '第一项*基本信息',
icon: Edit
},
{
total: '第二项*重要信息',
icon: Upload
},
{
total: '第三项*再次确认',
icon: Picture
}
])
let active = ref(1)
let form1 = ref([])
let formContent = ref([])
const nextSetp = (formRef, form) => {
formRef.validate((valid: any) => {
if (valid) {
active.value++
Object.assign(form1.value, { ...form })
console.log('表单', form1.value)
} else {
console.log('error submit!')
}
})
}
const prevSetp = () => {
active.value--
}
const submitSetp = (formRef, form) => {
formRef.validate((valid: any) => {
if (valid) {
Object.assign(formContent.value, { ...form1.value, ...form })
console.log('form', formContent.value)
} else {
console.log('error submit!')
}
})
}
</script>
child1组件
<template>
<el-form :model="form1" label-width="auto" style="max-width: 600px" :rules="rules" ref="formRef">
<el-form-item label="户名" prop="name">
<el-input v-model="form1.name" placeholder="校验字符长度"></el-input>
</el-form-item>
<el-form-item label="电话" prop="tel">
<el-input v-model="form1.tel" placeholder="正则校验"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="form1.password" placeholder="自定义校验"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model.number="form1.age" placeholder="请输入年龄"></el-input>
</el-form-item>
<el-button style="margin-top: 12px" @click="next()">下一步</el-button>
</el-form>
</template>
<script lang="ts" setup>
import { ref, reactive, watch, onMounted } from 'vue'
import type { FormInstance } from 'element-plus'
const emit = defineEmits()
const formRef = ref<FormInstance>()
const form1 = reactive({
name: '',
tel: '',
password: '',
age: ''
})
const rules = reactive({
name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
tel: [
{ required: true, message: '请输入联系方式', trigger: 'blur' },
{
pattern: /^1[3-9]\d{9}$/,
message: '手机号格式错误'
}
],
password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
bankCardNo: [{ required: true, message: '请输入银行卡号', trigger: 'blur' }]
})
const next = () => {
emit('nextSetp', formRef.value, form1)
}
child2
<template>
<el-form :model="form2" label-width="auto" style="max-width: 600px" :rules="rules" ref="formRef">
<el-form-item label="姓名" prop="xm">
<el-input v-model="form2.xm" placeholder="校验字符长度"></el-input>
</el-form-item>
<el-form-item label="电话" prop="dh">
<el-input v-model="form2.dh" placeholder="正则校验"></el-input>
</el-form-item>
<el-form-item label="支付密码" prop="mm">
<el-input v-model="form2.mm" placeholder="自定义校验"></el-input>
</el-form-item>
<el-form-item label="银行卡号" prop="kh">
<el-input v-model="form2.kh" placeholder="校验"></el-input>
</el-form-item>
<el-button style="margin-top: 12px" @click="prev(formRef)"> 上一步 </el-button>
<el-button style="margin-top: 12px" @click="next(formRef)">下一步</el-button>
</el-form>
</template>
<script lang="ts" setup>
import { ref, reactive, watch, onMounted } from 'vue'
import type { FormInstance } from 'element-plus'
const emit = defineEmits()
const formRef = ref<FormInstance>()
const form2 = reactive({
xm: '',
dh: '',
mm: '',
kh: ''
})
const next = () => {
emit('nextSetp', formRef.value, form2)
}
const prev = formRef => {
emit('prevSetp', formRef.value)
}
child3组件
<template>
<el-form :model="form3" label-width="auto" style="max-width: 600px" :rules="rules" ref="formRef">
<el-form-item label="姓名" prop="xx">
<el-input v-model="form3.xx" placeholder="校验字符长度"></el-input>
</el-form-item>
<el-form-item label="电话" prop="dd">
<el-input v-model="form3.dd" placeholder="正则校验"></el-input>
</el-form-item>
<el-button style="margin-top: 12px" @click="prev()"> 上一步 </el-button>
<el-button style="margin-top: 12px" @click="submit()">完成</el-button>
</el-form>
</template>
<script lang="ts" setup>
import { ref, reactive, watch, onMounted } from 'vue'
import nextBtn from './nextBtn.vue'
import type { FormInstance } from 'element-plus'
const emit = defineEmits()
const formRef = ref<FormInstance>()
const form3 = reactive({
xx: '',
dd: ''
})
const submit = () => {
emit('submitSetp', formRef.value, form3)
}
const prev = () => {
emit('prevSetp', formRef.value, form3)
}