element-ui 对话框模板

473 阅读1分钟

element-ui 对话框模板

父组件中使用子组件 并传递

isShow (是否开启Dirlog对话框)

title (弹窗名字)

@close (close事件关闭弹窗)

@sure (点击确认并且表单已经校验完成后要执行的函数--传递了一个参数form对象)

    <newEmpDirlogVue
      title="新增员工"
      :is-show="isShow"
      @close="isShow =false"
      @sure="sureFn"
    />
<template>
  <el-dialog :title="title" :visible.sync="isShow" width="50%" :before-close="beforeCloseFn" @closed="closedFn">
    <!-- form -->
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" style="width:300px" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancelFn">取 消</el-button>
      <el-button type="primary" @click="sureFn">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    isShow: { type: Boolean, default: false },
    title: { type: String, default: '' }
  },
  data() {
    return {
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        //   { validator: validateName, trigger: 'blur' }
        ]
      }
    }
  },

  watch: {
    isShow: { // 侦听isShow
      immediate: true,
      handler: function() {
        //   123
      }
    }
  },
  methods: {
    //   点确定
    sureFn() {
      this.$refs.form.validate((valid) => {
        if (!valid) return false
        this.$emit('sure', { ...this.form })
        this.close()
      })
    },
    // 点取消
    cancelFn() {
      this.close()
    },
    // 关闭方法
    close() {
      this.$emit('close')
    },
    // Dialog 关闭动画结束时的回调
    closedFn() {
      this.form = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
    },
    // 关闭前的回调
    beforeCloseFn() {
      this.close()
    }
  }
}
</script>


为了方便自己,不需要每次都要看手册,搞个模版