vue使用element-ui的必填表单和对话弹框,
时间: 2023-10-16 10:34:54 浏览: 105
可以通过以下的代码实现:
1. 必填表单的实现:
```html
<el-form :model="form" ref="form" :rules="rules">
<el-form-item label="姓名" prop="name">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model="form.age"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('form')">提交</el-button>
</el-form-item>
</el-form>
```
```javascript
data() {
return {
form: {
name: '',
age: ''
},
rules: {
name: [
{ required: true, message: '请输入姓名', trigger: 'blur' }
],
age: [
{ required: true, message: '请输入年龄', trigger: 'blur' }
]
}
}
},
methods: {
submitForm(formName) {
this.$refs[formName].validate(valid => {
if (valid) {
// 表单验证通过
} else {
// 表单验证失败
}
})
}
}
```
2. 对话框的实现:
```html
<el-button type="primary" @click="dialogVisible = true">打开对话框</el-button>
<el-dialog title="对话框标题" :visible.sync="dialogVisible">
<span>对话框内容</span>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="dialogVisible = false">确定</el-button>
</div>
</el-dialog>
```
```javascript
data() {
return {
dialogVisible: false
}
}
```
阅读全文