vue中添加或者修改方法

<el-dialog></el-dialog>用于点击按钮弹出对话框

其中的属性 :title 用于判断 不加:号的话是直接显示内容
visible用于是否将页面显示出来;
点击添加或者修改方法之后直接调用init()方法:
方法中先将表单内容清空,然后将窗口打开,拿到表单中的id值,根据id去后台拿到整个数据
在修改页面将数据回显在页面
在表单提交方法中根据有无id去进行判断该调用修改方法还是添加方法
成功失败之后调用各自方法




<template> <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible"> <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px"> <el-row> <el-col :span="12"> <el-form-item label="常用语" prop="content"> <el-input v-model="dataForm.content" placeholder="姓名"></el-input> </el-form-item> </el-col> </el-row> </el-form> <span slot="footer" class="dialog-footer"> <el-button @click="visible = false">取消</el-button> <el-button type="primary" @click="dataFormSubmit()">确定</el-button> </span> </el-dialog> </template> <script> export default { data () { return { dataForm: { id: 0, content: '' }, visible: false, dataRule: { content: [ { required: true, message: '内容不能为空', trigger: 'blur' } ] } } }, methods: { init (id) { this.dataForm = {} this.visible = true this.dataForm.id = id if (id) { this.$http({ url: this.$http.adornUrl(`/cyy/apply/info/${this.dataForm.id}`), method: 'get', params: this.$http.adornParams() }).then(({data}) => { if (data && data.code === 0) { this.dataForm = data.cyy } }) } }, // 表单提交 dataFormSubmit () { this.$refs['dataForm'].validate((valid) => { if (valid) { this.$http({ url: this.$http.adornUrl(`/cyy/apply/${this.dataForm.id ? 'updateCyy' : 'addCyy'}`), method: 'post', data: JSON.stringify(this.dataForm) }).then(({data}) => { if (data && data.code === 0) { this.$message({ message: '操作成功', type: 'success', duration: 1500, onClose: () => { this.visible = false this.$emit('refreshDataList') } }) } else { this.$message.error(data.msg) } }) } }) } } } </script>
原文地址:https://www.cnblogs.com/BKhp/p/11507665.html