欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

ElementUI:关闭对话框,使用消息框来实现 - vue2 的写作风格

最编程 2024-03-05 13:12:38
...

那么我就按照官网的写法来做:

<template>
  <el-button @click="open">打开弹窗</el-button>
</template>
<script>
export default {
  methods: {
    async open() {
      let sex = '未知'
      const h = this.$createElement
      await this.$msgbox({
        title: '提示',
        message: h('p', [
          '选择性别',
          h('el-select', {
            props: {
              value: sex
            },
            on: {
              change: val => (sex = val)
            },
            style: {
              marginLeft: '1em'
            }
          }, ['男', '女', '未知'].map(sex => {
            return h('el-option', {
              key: sex,
              props: {
                label: sex,
                value: sex
              }
            })
          }))
        ])
      })
      alert(sex)
    }
  }
}
</script>

效果图:

20211129171032.jpg

可是问题出现了,改变不了select的值???

屏幕录制2021-11-29 下午5.15.01.gif

百思不得其姐,那只好去看看element源码呗,发现原来是将message赋值给$slots.default

image.png

那么我立刻就想到message是vnode的话,何不去用VueComponent去试试呢?那么就可以改造成:

<template>
  <el-button @click="open">打开弹窗</el-button>
</template>
<script>
import Vue from 'vue'

const MessageboxSelect =  Vue.component('messagebox-select', {
  data() {
    return {
      value: '未知'
    }
  },
  render() {
    const h = this.$createElement
    return h('p', [
      '选择性别',
      h('el-select', {
        props: {
          value: this.value
        },
        on: {
          change: val => (this.value = val)
        },
        style: {
          marginLeft: '1em'
        }
      }, ['男', '女', '未知'].map(sex => {
        return h('el-option', {
          key: sex,
          props: {
            label: sex,
            value: sex
          }
        })
      }))
    ])
  }
})

export default {
  methods: {
    async open() {
      const h = this.$createElement
      await this.$msgbox({
        title: '提示',
        message: h(MessageboxSelect)
      })
    }
  }
}
</script>

但是这样会引发一个问题,弹窗消失后再打开会出现之前保留的状态,而不是重新生成初始状态

屏幕录制2021-11-29 下午9.41.01.gif

那么就好办了,只要把Vue.component()这一步放到open里面就可以了

<template>
  <el-button @click="open">打开弹窗</el-button>
</template>
<script>
import Vue from 'vue'

export default {
  methods: {
    async open() {
      let sex = '未知'
      const h = this.$createElement
      await this.$msgbox({
        title: '提示',
        message: h(Vue.component('messagebox-select', {
          data() {
            return {
              value: sex
            }
          },
          render() {
            const h = this.$createElement
            return h('p', [
              '选择性别',
              h('el-select', {
                props: {
                  value: this.value
                },
                on: {
                  change: val => (sex = this.value = val)
                },
                style: {
                  marginLeft: '1em'
                }
              }, ['男', '女', '未知'].map(sex => {
                return h('el-option', {
                  key: sex,
                  props: {
                    label: sex,
                    value: sex
                  }
                })
              }))
            ])
          }
        }))
      })
      alert(sex)
    }
  }
}
</script>

屏幕录制2021-11-29 下午9.49.33.gif

搞掂!!