Vue3.x + Ant Design Vue 登录表单示例代码
<template>
<div class='login-form'>
<a-form :form='form' @submit='handleSubmit'>
<a-form-item>
<a-input
v-model='username'
:placeholder='$t('login.username')'
:allow-clear='true'
prefix-icon='user'
:rules='[
{ required: true, message: $t('login.usernameRequired') },
{ min: 4, max: 16, message: $t('login.usernameLength') },
]'
/>
</a-form-item>
<a-form-item>
<a-input-password
v-model='password'
:placeholder='$t('login.password')'
:allow-clear='true'
prefix-icon='lock'
:rules='[
{ required: true, message: $t('login.passwordRequired') },
{ min: 6, max: 20, message: $t('login.passwordLength') },
]'
/>
</a-form-item>
<a-form-item>
<a-button
type='primary'
:loading='loading'
:disabled='!form.validate()'
html-type='submit'
>
{{ $t('login.login') }}
</a-button>
</a-form-item>
</a-form>
</div>
</template>
<script>
import { defineComponent, ref } from 'vue'
import { Form, Input, Button } from 'ant-design-vue'
export default defineComponent({
name: 'LoginForm',
components: { Form, FormItem: Form.Item, Input, InputPassword: Input.Password, Button },
setup(props, { emit }) {
const form = ref(null)
const username = ref('')
const password = ref('')
const loading = ref(false)
const handleSubmit = async () => {
loading.value = true
try {
await form.value.validateFields()
emit('submit', { username: username.value, password: password.value })
} finally {
loading.value = false
}
}
return {
form,
username,
password,
loading,
handleSubmit,
}
},
})
</script>
<style scoped>
.login-form {
max-width: 320px;
margin: 0 auto;
}
</style>
AI 优化版
原文地址:https://www.cveoy.top/t/topic/nnuG 著作权归作者所有。请勿转载和采集!