如何使用Vue封装弹框组件

这是封装组件

<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)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,可以使用插槽封装弹框组件。以下是一个示例: ``` <template> <div> <button @click="showModal = true">打开弹框</button> <transition name="modal"> <div v-if="showModal" class="modal-mask"> <div class="modal-wrapper"> <div class="modal-container"> <div class="modal-header"> <slot name="header">默认标题</slot> <button class="modal-close" @click="showModal = false">X</button> </div> <div class="modal-body"> <slot name="body">默认内容</slot> </div> <div class="modal-footer"> <slot name="footer"> <button @click="showModal = false">关闭</button> </slot> </div> </div> </div> </div> </transition> </div> </template> <script> export default { data() { return { showModal: false } } } </script> ``` 这个组件包含了一个按钮,当用户点击按钮时,弹出一个模态框。模态框的内容由插槽来提供。组件模板中定义了三个插槽,分别是header、body和footer。如果用户没有提供插槽内容,那么组件就会显示默认内容。如果用户提供了插槽内容,那么组件就会显示用户提供的内容。 这个组件使用Vue的过渡效果来实现弹框的动画效果。当弹框出现时,会有一个淡入的效果;当弹框关闭时,会有一个淡出的效果。这个过渡效果的名称是“modal”,可以在组件的样式中定义。 这个组件使用方法如下: ``` <template> <div> <my-modal> <template v-slot:header> <h2>自定义标题</h2> </template> <template v-slot:body> <p>自定义内容</p> </template> <template v-slot:footer> <button @click="showModal = false">确认</button> </template> </my-modal> </div> </template> <script> import MyModal from './MyModal.vue' export default { components: { MyModal }, data() { return { showModal: false } } } </script> ``` 在这个示例中,我们使用了自定义组件MyModal,并为它提供了三个插槽:header、body和footer。我们在这些插槽中提供了自定义的内容,以替换默认的内容。最后,我们将MyModal组件包裹在一个div中,并将这个div显示在页面上。当用户点击按钮时,弹框就会出现,并显示用户提供的内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值