vue自己封装组件(通过具名插槽、组件传参 封装的一个属于自己的弹窗组件)

1. 首先,将自己想要封装的组件创建好。(使用插槽、组件传参来控制这个弹窗的显示和隐藏,以及弹窗的内容)

<template>
	<div id="qf-modal" v-if="visible">
		<!-- 封装一个弹窗的组件 -->
		<!-- 通过插槽来控制弹框展示的内容 -->
		<div class="mask">
			<div class="wrap">
				<!-- 具名插槽 -->
				<slot name="title">
					<!--在这个具名插槽中的内容为 具名插槽的默认值 当在父组件中没有该内容时,则使用这个默认的值-->
					<div class="title">
						警告
					</div>
				</slot>
				<slot name="body">
					<div class="body">
						你确定要购买吗
					</div>
				</slot>
				<div class="wrap__button">
					<button class="model__button--confirm" @click="confirm">确定</button>
					<button class="model__button--cancel" @click="cancel">取消</button>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	//1.需要一个属性,控制组件的显示和隐藏,接受一个visible属性来控制显示和隐藏
	//2.需要动态的接受title body button,如果没有传入的时候,采用默认的结构
	//3.点击确定或者是取消按钮的时候需要把模态框关掉,预留方法
	//方法名confirm点击确定的处理函数
	//方法名cancel点击取消的处理函数
	export default {
		data() {
			return {}
		},
		props: { //通过props来接受这个visible属性
			visible: { //表示对visible进行校验
				type: Boolean, //表示这个visible的类型要为布尔值
				default () { //表示默认值 
					return false //默认为false
				}
			}
		},
		methods: {
			// 将封装的组件中我们定义的两个方法,进行预留,通过$emit传递出去
			confirm() { //点击确定的操作
				this.$emit('confirm') 
			}, //表示将on-confirm发送出去 类似于发布事件
			cancel() { //点击取消的操作
				this.$emit('cancel')
			}
		}
	}
</script>
<style scoped>
	.mask {
		position: fixed;
		background-color: rgba(0, 0, 0, 0.3);
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.wrap {
		width: 350px;
		background-color: #fff;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		border-radius: 10px;
	}
	.wrap div {
		margin: 15px 0;
	}
	.model__button--confirm {
		margin: 0 5px;
		outline: none;
		border: 0;
		width: 150px;
		height: 30px;
		background: linear-gradient(90deg, #1596fb, pink);
		border-radius: 0.2847rem;
		display: block;
		color: #fff;
		cursor: pointer;
	}
	.model__button--cancel {
		outline: none;
		border: 0;
		width: 150px;
		height: 30px;
		background: linear-gradient(90deg, blue, green);
		border-radius: 0.2857rem;
		display: block;
		color: #fff;
		cursor: pointer;
	}
	.wrap__button {
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>

2. 在组件中,引入封装的组件,在页面上展示这个组件

<template>
	<div id="app">
		<!-- 通过按钮来控制封装的组件的展示和关闭 -->
		<button type="button" @click="open">打开</button>
		<!-- 展示封装的组件 -->
		<!-- 我们通过组件之间的传值,以及对插槽的运用,就可以实现我们对封装的组件的最大的使用 -->
		<modal :visible='isShow' @confirm='confirm' @cancel='cancel'>
			<template v-slot="title"> //此处的v-slot的名字要和我们在封装的组件中的插槽的名字一致
				<h1>自己定义的标题</h1> //如果此处不写内容,那么就是使用我们组件默认的内容
			</template>
			<template v-slot="body"> //此处的v-slot的名字要和我们在封装的组件中的插槽的名字一致
				自己定义的弹窗的内容  //如果此处不写内容,那么就是使用我们组件默认的内容
			</template>
		</modal>
	</div>
</template>

<script>
	import modal from './modal.vue' //引入封装的组件
	export default {
		name:'app',
		components:{ //将封装的组件注册成为自己的子组件
			modal
		},
		data(){
			return { 
				isShow:false
			}
		},
		methods:{
			open(){
				this.isShow = true
			}
			confirm(){
				console.log('打开弹框')
				this.isShow = true
			},
			cancel(){
				console.log('关闭弹窗')
				this.isShow = false
			}
		}
	}
</script>

<style>
</style>

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一款流行的JavaScript框架,它在构建用户界面方面非常强大。以下是一个示例代码,用于封装一个基本的弹窗组件: ```javascript <template> <div> <button @click="showModal">打开弹窗</button> <div v-if="isOpen" class="modal"> <div class="modal-content"> <slot></slot> <button @click="closeModal">关闭弹窗</button> </div> </div> </div> </template> <script> import { ref } from 'vue'; export default { name: 'Modal', setup() { const isOpen = ref(false); const showModal = () => { isOpen.value = true; }; const closeModal = () => { isOpen.value = false; }; return { isOpen, showModal, closeModal }; }, }; </script> <style scoped> .modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .modal-content { background-color: white; padding: 20px; } button { margin-top: 10px; } </style> ``` 这个弹窗组件包含了一个按钮和一个模态框。当点击按钮时,模态框会显示出来,并且渲染出插槽内容。关闭按钮可以用于关闭模态框。在模态框外部点击也可以关闭模态框。 这个组件使用Vue3的Composition API来定义逻辑。通过ref函数创建一个响应式引用isOpen,用于跟踪模态框的开启和关闭状态。showModal方法用于打开模态框,closeModal方法用于关闭模态框。使用slot插槽来动态渲染弹窗内容。 在样式上,将模态框设置为固定定位,并使用背景色来实现半透明遮罩效果。模态框内容使用白色背景并设置内边距。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值