vue使用elementui组件的的对话框;使用ref

时间:2024-04-08 19:00:46

 1.第一步,先在父组件中引用,设置ref的值

<el-dialog :visible.sync="dialogEditVisible"
 append-to-body width="1000px" title="编辑"
 :close-on-click-modal="false">
 <dialog-edit v-if="dialogEditVisible" ref="dialogEdit"
  :visible.sync="dialogEditVisible" />
</el-dialog>

 2.引入和注册组件:

import DialogEdit from './edit'
export default {
  components: { Pagination,DialogAdd,DialogEdit,DialogView},

3.需要再data中设置对话框默认不打开:

 dialogEditVisible: false,

4.在父组件中mothod中实现调用对话框的方法:

handleEdit(scope) {
      this.dialogEditVisible = true
      this.$nextTick(() => {
        //this.$refs.dialogEdit
        this.$refs.dialogEdit.getDetailed()
      })
    },

5.创建一个vue组件,名字为edit.vue,并且在method中添加getDetailed()   ,这个方法:

getDetailed() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      getInfo({ code: this.proid }).then(response => {
        this.form = response.data.mdProjectInfo,
        loading.close()
      }).catch(function() {
        loading.close()
      })
    },

<template>
  <div style="width:100%;height:auto; margin:0 auto;">
    <el-form :model="form" label-width="100px" label-position="left">
      <el-row :gutter="20">
        <el-col :span="16">
          <el-form-item>
            <!-- <div slot="label">项目名称<font color="red">*</font></div>           
            <el-select v-model="form.dirId" clearable style="width:100%;" @change="selectGoodsByGroupId($event)">
              <el-option v-for="item in symbols" :key="item.id" :label="item.name" :value="item.id" />
            </el-select> -->
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <div slot="label">问题标题<font color="red">*</font></div>
            <el-input v-model="form.name" placeholder="问题标题" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item>
            <div slot="label">问题类型<font color="red">*</font></div>
            <el-input v-model="form.staffId" placeholder="问题类型" />
          </el-form-item>
        </el-col>  
      <el-col :span="8">
        <el-form-item>
          <div slot="label">优先级<font color="red">*</font></div>
            <el-input v-model="form.staffId" placeholder="优先级" />
          </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item>
          <div slot="label">问题状态<font color="red">*</font></div>
            <el-input v-model="form.staffId" placeholder="问题状态" />
          </el-form-item>
      </el-col>
      </el-row>
      <el-form-item label="内容">
        <el-input v-model="form.content" type="textarea" :autosize="{ minRows: 3, maxRows: 6}" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.message" type="textarea" :autosize="{ minRows: 3, maxRows: 6}" />
      </el-form-item>
    </el-form>
    <div style="text-align:center;">
      <el-button type="primary" @click="save()">保存</el-button>
      <el-button type="danger" @click="closePage">取消</el-button>
    </div>
  </div>
</template>
<script>
 import { saveData,getInfo,edit} from '@/api/records'
 import { getAllList } from '@/api/maApplicationInfo'
import { Message } from 'element-ui'

export default {
  inject: ['getList'],
  props: { // 第二种方式
    proid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {},
      types: [],
      symbols: []
    }
  },
 created() {     
    this.getproject()
  },
  methods: {
    getDetailed() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      getInfo({ code: this.proid }).then(response => {
        this.form = response.data.mdProjectInfo,
        loading.close()
      }).catch(function() {
        loading.close()
      })
    },
    getproject(){
      getAllList().then(response=> {
           this.symbols = response.data
       })
    },
    selectGoodsByGroupId(val) { // 根据设备组id获取相应的商品
      if (val != null && val !== '' && val !== undefined) {
        for (var i = 0; i < this.symbols.length; i++) {
          if (this.symbols[i].id === val) {
            this.form.userId = this.symbols[i].customerMid
          }
        }
      }
  },
    save(){
      this.form.startTime = this.form.itemTime[0]
      this.form.endTime = this.form.itemTime[1]
      const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      edit(this.form).then(response => {
      
          Message({
            message: '编辑成功',
            type: 'success',
            duration: 5 * 1000
          })
          this.$emit('update:visible', false)
          loading.close()
          this.getList()
        }).catch(response => {
          loading.close()
          this.getList()
        })
    },
    closePage() {
      this.$emit('update:visible', false)
    }
  }
}
</script>