如何使用Vue封装弹框组件

本文深入探讨Vue中封装组件的方法,重点讲解如何利用Slot机制传递数据,实现组件间的通信和自定义内容嵌入,通过实例展示如何创建可复用性强的组件。

这是封装组件

<template>
    <div class="dialog">
        <div class="dialog-box">
        	// 其调用的组件slot传值问题,没有的话是显示默认值
            <slot name="title">如果没传默认title</slot>
            <div class="dialog-content">
              <slot name="content">如果没传默认content</slot>
            </div>
            <div class="dialog-btns">
                <button @click="qd">确定</button>
                <button @click="qx">取消</button>
            </div>
        </div>
    </div>
</template>
<script>
import Bus from "../../utils/Bus"
export default {
  methods: {
    qd()
    {
     //确认按钮,这里使用this.$emit派发事件让其调用的组件触发事件
      this.$emit("qd")
      // Bus.$emit()
      //这里也可以使用Bus.$emit()在点击确认之前传送写什么数据
    },
    qx()
    {
    //取消按钮,这里使用this.$emit派发事件让其调用的组件触发事件
      this.$emit("qx")
    }
  }
}
</script>

样式自行模拟…

这是其调用的组件(父组件)

<template>
  <div>
	<p @click="save()">显示隐藏</p>
	<Dlog  
		v-show="flag"
		@qd="qd"
		@qx="qx"
		//:num="item.rate" 这里可以给封装的组件传数据,看需求了
	>
		// 这里可以使用slot去传数据
		<h1 slot="title">我是调用它的组件传来的标题</h1>
		<h1 slot="content">我是调用它的组件传来的内容</h1> -->
	</Dlog>
  </div>
</template>

<script>
import Dlog from "@/components/dlog"
import Bus from "../utils/Bus"
export default {
	components:{Dlog},
	data()
	{
		return{
			flag:false
		}
	},
	methods:{
		//显示隐藏,也可以在封装的组件中去写
		save(){
			this.flag=!this.flag
		},
		//获取数据,随便写的本地json
		getData()
		{
			this.$axios.get("http://localhost:9999/data.json").then((res)=>{
				console.log(res.data.token)
			})
		},
		// 点击确定 
		qd()
		{
			console.log('在调用他的组件上点击了确定按钮')
			this.flag=false
			//在调用的组件上请求接口
			this.getData();
			// 这里点击确认后 接收数据
			Bus.$on('name',res=>{
				console.log(res)
				this.$axios.get("https//www.baidu.com?name="+res)
			})
		},
		// 点击取消
		qx()
		{
			console.log('在调用他的组件上点击了取消按钮')
			this.flag=false
		}
	},
}
</script>

样式自行模拟

这里呢写的也比较简单,具体的话看代码需求了,毕竟没有需求,只能自己模拟去写
大体思路就是:
1.你想向用户暴露哪些属性 (props)
2.你想向用户暴露哪些事件,让用户监听来处理后续的业务 ($emit,@)
3.你想让用户嵌入哪些自定义的内容(slot)

### Vue3 中封装组件 #### 创建父级组件并引入子组件 为了实现一个可重用的组件,在父组件中定义控制窗显示与否的状态变量 `isPopUp` 并设置相应的方法来改变这个状态。当用户点击按钮时,通过修改该布尔值决定是否展示窗。 ```javascript import Popup from './Popup.vue' export default { components: { Popup }, data() { return { isPopUp: false, } }, methods: { onPopUp() { this.isPopUp = true; }, onConfirm(val) { console.log(val); this.isPopUp = false; }, onCancel() { this.isPopUp = false; } } } ``` 上述代码展示了如何在父组件内管理窗可见性的逻辑[^2]。 #### 定义子组件结构与样式 接下来设计具体的窗界面布局以及交互行为。这里采用简单的 HTML 和 CSS 来构建基本外观,并利用事件监听器处理用户的操作反馈。 ```html <template> <!-- 窗遮罩层 --> <transition name="fade"> <div class="popup-mask" v-if="visible"> <div class="popup-wrapper"> <div class="popup-container"> <!-- 关闭按钮 --> <button @click="$emit('close')">X</button> <!-- 自定义内容区域 --> <slot></slot> <!-- 底部操作栏 --> <footer> <button @click="$emit('confirm', '确认')">确定</button> <button @click="$emit('cancel')">取消</button> </footer> </div> </div> </div> </transition> </template> <script> export default { props: ['modelValue'], emits: ['update:modelValue', 'confirm', 'cancel'], computed: { visible: { get() { return this.modelValue; }, set(value) { this.$emit('update:modelValue', value); } } } }; </script> <style scoped> .popup-mask { position: fixed; z-index: 9998; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, .5); display: table; } /* ...其他CSS省略... */ </style> ``` 这段代码实现了带有关闭功能的基础窗模板,并且支持自定义插槽用于放置特定的内容;同时提供了两个底部按钮分别对应不同的回调动作[^4]。 #### 使用v-model双向绑定属性 为了让父子组件之间的通信更加简洁明了,可以借助 `v-model` 实现数据的双向绑定效果。这样不仅简化了 API 设计,也提高了开发效率。 ```html <!-- 父组件中的调用方式 --> <Popup v-model="isPopUp"></Popup> ``` 这种方式允许开发者直接通过单个 prop 控制窗的显隐状态变化,而不需要单独传递打开/关闭方法给子组件[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值