表单高级用法
表单初始化
效果预览可直接操作下方示例
正在加载示例…
<template>
<el-button type="primary"
@click="handleReload">Key初始化</el-button>
<avue-form :key="reload"
:option="option"></avue-form>
</template>
<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
const reload = ref(Math.random());
const option = {
border: true,
align: 'center',
menuAlign: 'center',
column: [
{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '省份',
prop: 'province',
type: 'select',
props: {
label: 'name',
value: 'code'
},
dicUrl: `https://api.avuejs.com/area/getProvince`,
rules: [
{
required: true,
message: '请选择省份',
trigger: 'blur'
}
]
}
]
};
function handleReload () {
reload.value = Math.random();
ElMessage.success('初始化完成');
}
</script>
配置项服务端加载
提示
- 这里没有走真真的服务器请求,而是做了一个模拟
效果预览可直接操作下方示例
正在加载示例…
<template>
<el-button type="primary"
icon="el-icon-sort"
@click="getOption">服务端加载</el-button>
<avue-form :key="reload"
v-model="form"
:option="option"
v-loading="loading"></avue-form>
</template>
<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
const reload = ref(Math.random());
const loading = ref(true);
const form = ref({});
const option = ref({});
function getOption () {
ElMessage.success('模拟2s后服务端动态加载');
setTimeout(() => {
option.value = {
border: true,
align: 'center',
menuAlign: 'center',
column: [
{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '省份',
prop: 'province',
type: 'select',
props: {
label: 'name',
value: 'code'
},
dicUrl: `https://api.avuejs.com/area/getProvince`,
rules: [
{
required: true,
message: '请选择省份',
trigger: 'blur'
}
]
}
]
};
form.value = {
name: '张三',
sex: '男',
province: '110000'
};
reload.value = Math.random();
loading.value = false;
}, 2000);
}
</script>
配置项切换
效果预览可直接操作下方示例
正在加载示例…
<template>
<el-button type="primary"
icon="el-icon-sort"
@click="handleSwitch">切 换</el-button>
<br /><br />
<avue-form :key="reload"
v-model="form"
:option="option"></avue-form>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const reload = ref(Math.random());
const type = ref(true);
const form = ref({
name: '张三',
sex: '男',
username: 'smallwei',
password: 'smallwei'
});
const option = ref({});
const option1 = {
addBtn: false,
column: [
{
label: '昵称',
prop: 'name'
}
]
};
const option2 = {
addBtn: false,
column: [
{
label: '用户名',
prop: 'username'
},
{
label: '密码',
prop: 'password',
type: 'password'
},
{
label: '姓名',
prop: 'name'
}
]
};
function handleSwitch () {
type.value = !type.value;
if (type.value) {
option.value = option1;
} else {
option.value = option2;
}
reload.value = Math.random();
}
onMounted(() => {
handleSwitch();
});
</script>
