// 示例代码:使用 Vue 和 Ant Design 实现一个简单的表单
<template>
<a-form :form="form" @submit="handleSubmit">
<a-form-item label="用户名">
<a-input v-decorator="['username', { rules: [{ required: true, message: '请输入用户名!' }] }]" />
</a-form-item>
<a-form-item label="密码">
<a-input-password v-decorator="['password', { rules: [{ required: true, message: '请输入密码!' }] }]" />
</a-form-item>
<a-form-item>
<a-button type="primary" html-type="submit">
提交
</a-button>
</a-form-item>
</a-form>
</template>
<script>
export default {
data() {
return {
form: this.$form.createForm(this, { name: 'login_form' }),
};
},
methods: {
handleSubmit(e) {
e.preventDefault();
this.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
});
},
},
};
</script>
<style scoped>
/* 可以在这里添加一些样式 */
</style>
模板部分 (<template>
):
a-form
和 a-form-item
组件来创建一个表单。a-input
和 a-input-password
分别用于输入用户名和密码。v-decorator
指令用于绑定表单项的验证规则,确保用户输入符合要求。脚本部分 (<script>
):
data
函数中初始化了一个表单实例 form
。handleSubmit
方法在表单提交时被调用,它会验证表单字段并输出有效数据。样式部分 (<style scoped>
):
scoped
属性确保样式仅应用于当前组件。上一篇:vue3 调用子组件方法
下一篇:vue eventbus
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站