用vue2和antdv写一个注册页面 其中包含登录名 昵称 密码 手机号 头像 都是必填项
时间: 2023-05-17 08:06:44 浏览: 282
好的,以下是一个用vue2和antdv写的注册页面的示例代码:
<template>
<div>
<a-form :form="form" @submit="handleSubmit">
<a-form-item label="登录名" :colon="false" :required="true">
<a-input v-decorator="['username', { rules: [{ required: true, message: '请输入登录名' }] }]" />
</a-form-item>
<a-form-item label="昵称" :colon="false" :required="true">
<a-input v-decorator="['nickname', { rules: [{ required: true, message: '请输入昵称' }] }]" />
</a-form-item>
<a-form-item label="密码" :colon="false" :required="true">
<a-input-password v-decorator="['password', { rules: [{ required: true, message: '请输入密码' }] }]" />
</a-form-item>
<a-form-item label="手机号" :colon="false" :required="true">
<a-input v-decorator="['phone', { rules: [{ required: true, message: '请输入手机号' }] }]" />
</a-form-item>
<a-form-item label="头像" :colon="false" :required="true">
<a-upload
v-decorator="['avatar', { rules: [{ required: true, message: '请上传头像' }] }]"
:before-upload="beforeUpload"
:show-upload-list="false"
>
<a-avatar :src="avatarUrl" :size="64" />
</a-upload>
</a-form-item>
<a-form-item>
<a-button type="primary" html-type="submit">注册</a-button>
</a-form-item>
</a-form>
</div>
</template>
<script>
import { FormModel, Input, InputPassword, Button, Upload, Avatar } from 'ant-design-vue'
export default {
name: 'RegisterForm',
components: {
'a-form': FormModel,
'a-form-item': FormModel.Item,
'a-input': Input,
'a-input-password': InputPassword,
'a-button': Button,
'a-upload': Upload,
'a-avatar': Avatar,
},
data() {
return {
form: this.$form.createForm(this),
avatarUrl: '',
}
},
methods: {
handleSubmit(e) {
e.preventDefault()
this.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values)
}
})
},
beforeUpload(file) {
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
if (!isJpgOrPng) {
this.$message.error('只能上传 JPG/PNG 格式的文件!')
return false
}
const isLt2M = file.size / 1024 / 1024 < 2
if (!isLt2M) {
this.$message.error('图片大小不能超过 2MB!')
return false
}
this.getBase64(file, (imageUrl) => {
this.avatarUrl = imageUrl
this.form.setFieldsValue({ avatar: file })
})
return false
},
getBase64(file, callback) {
const reader = new FileReader()
reader.addEventListener('load', () => callback(reader.result))
reader.readAsDataURL(file)
},
},
}
</script>
注意:这只是一个示例代码,实际应用中可能需要根据具体需求进行修改。
阅读全文