Vue封装一个Dialog(对话框)组件

前几天被人问到过,当时挺懵的,回来看了看,发现原来是这样做的

效果图如下,只实现了一个最基本的,可以传入属性,也可以使用方法,后续需要什么,再往里面加

  主要是通过v-model控制对话框的显示和隐藏,实现父子组件的双向数据绑定

  • v-model用在组件上,默认绑定的值为modelValue,相当于v-model:modelValue。当然也可以自定义值
  • 父组件中子组件标签里的内容,子组件用插槽进行接收

父组件

<template>
  <div class="dialog">
    <button @click.stop="showModel">显示对话框 -- {{ show }}</button>
    <Diglog
      v-model="show"
      :title="title"
      @open="handleOpen"
      @close="handleClose"
    >
      <p>插槽内容</p>
      <template #footer>
        <span>
          <button @click="show = false">确定</button>
          <button @click="show = false">取消</button>
        </span>
      </template>
    </Diglog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import Diglog from './components/Diglog.vue'
// 控制对话框显示/隐藏
let show = ref(false)
// 窗口标题
let title = ref('标题呀')

const showModel = () => {
  show.value = true
}

const handleOpen = (a) => {
  console.log('打开了' + '默认参数为' + a)
}

const handleClose = (a) => {
  console.log('关闭了' + '默认参数为' + a)
}
</script>

子组件

<template>
  <div class="box" ref="box" v-show="props.modelValue">
    <i class="close" @click="closeModel">X</i>
    <h3 class="title">{{ props.title }}</h3>
    <slot>插槽备用内容1</slot>
    <footer class="footer">
      <slot name="footer">插槽备用内容2</slot>
    </footer>
  </div>
</template>

<script setup>
import { defineProps, defineEmits, ref, watch } from 'vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: '',
  },
})

let box = ref(null)

const emits = defineEmits(['update:modelValue', 'open', 'close'])

document.addEventListener('click', function (e) {
  if (!box.value.contains(e.target)) {
    emits('update:modelValue', false)
  }
})

const closeModel = () => {
  emits('update:modelValue', false)
}

watch(
  // 这种写法会侦听到 props 中 test 的变化
  () => props.modelValue,
  (newValue) => {
    // 打开了
    if (newValue === true) {
      emits('open', '随便传个参数吧1')
    }
    // 关闭了
    if (newValue === false) {
      emits('close', '随便传个参数吧2')
    }
  }
)
</script>

<style scoped>
.box {
  position: relative;
  width: 500px;
  height: 300px;
  background-color: pink;
  margin: auto;
}

.title {
  text-align: center;
  height: 30px;
  background: skyblue;
}

.close {
  display: block;
  position: absolute;
  top: 0;
  right: 0;
  width: 30px;
  text-align: center;
  line-height: 30px;
  background-color: #ccc;
  cursor: pointer;
}

.footer {
  position: absolute;
  bottom: 0;
  right: 0;
}
</style>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,用于构建交互式Web应用程序。封装对话框组件是非常重要的,因为它可以使代码更加模块化和易于维护。下面是如何使用Vue封装对话框组件的步骤: 1.创建一个Vue组件来表示对话框。这个组件应该有一个模板,一个数据对象和一些方法来控制对话框的状态。例如: ``` <template> <div v-if="showDialog"> <div class="dialog-box"> <h2>{{ title }}</h2> <p>{{ message }}</p> <button @click="closeDialog">Close</button> </div> <div class="overlay" @click="closeDialog"></div> </div> </template> <script> export default { data() { return { showDialog: false, title: '', message: '', } }, methods: { openDialog(title, message) { this.title = title this.message = message this.showDialog = true }, closeDialog() { this.showDialog = false }, }, } </script> ``` 2.在需要使用对话框组件中,导入对话框组件并注册它: ``` <template> <div> <button @click="openDialog">Open Dialog</button> <Dialog v-if="showDialog" @close="closeDialog" :title="title" :message="message" /> </div> </template> <script> import Dialog from './Dialog.vue' export default { components: { Dialog }, data() { return { showDialog: false, title: '', message: '', } }, methods: { openDialog() { this.title = 'Hello' this.message = 'This is a dialog' this.showDialog = true }, closeDialog() { this.showDialog = false }, }, } </script> ``` 3.在需要使用对话框组件中,调用对话框组件的openDialog方法来打开对话框。例如: ``` this.$refs.dialog.openDialog('Hello', 'This is a dialog') ``` 4.如果需要在对话框关闭时执行一些操作,可以监听对话框组件的close事件。例如: ``` <Dialog v-if="showDialog" @close="onDialogClose" :title="title" :message="message" /> ... methods: { onDialogClose() { console.log('Dialog closed') }, } ``` 这样就可以使用Vue封装对话框组件了。在实际开发中,需要根据具体的需求来设计对话框组件的样式和功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值