用vue和element-ui写一个弹窗,显示校验成功
时间: 2023-05-13 19:05:30 浏览: 114
可以使用以下代码实现:
<template>
<el-dialog :visible.sync="dialogVisible">
<el-form :model="form" ref="form" :rules="rules" label-width="80px">
<el-form-item label="用户名" prop="username">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="form.password"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="submitForm('form')">确 定</el-button>
</div>
</el-dialog>
</template>
<script>
export default {
data() {
return {
dialogVisible: false,
form: {
username: '',
password: ''
},
rules: {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' }
]
}
}
},
methods: {
submitForm(formName) {
this.$refs[formName].validate(valid => {
if (valid) {
this.$message({
message: '校验成功',
type: 'success'
});
this.dialogVisible = false;
} else {
return false;
}
});
}
}
}
</script>
注意:需要在组件中引入 element-ui 库。
阅读全文