vue中自定义弹框show-modal

使用方式

在template中引入

<show-modal></show-modal>

在需要使用show-modal的地方使用

this.$showModal({
					title: '',
					content: '确认重置,重置将删除所有数据',
					cancelColor:"#4B557B",
					cancelBackgroundColor:"#EFF2FF",
					cancelText:"取消",
					confirmColor:"#FFFFFF",
					confirmBackgroundColor:"#4B557B",
					confirmText:"确认",
					success:(res)=>{
						if (res.confirm) {
						  console.log('用户点击确定')
						} else if (res.cancel) {
						  console.log('用户点击取消')
						}			
					}
				})

在main.js中挂在组件

import initModal from "@/components/show-modal/initModal.js"
import showModal from "@/components/show-modal/show-modal.vue"
initModal(Vue)
Vue.component('show-modal',showModal);

文件代码:组件show-modal

show-modal/show-modal.vue

<template>
	<div class="_showModal" v-show="show">
			<div class="_shade"></div>
			<div class="_modalBox">
				<div class="_modal">
					<slot name="title">
						<div class="title" v-show="title">{{title}}</div>
					</slot>
					<slot name="content">
						<div class="content">{{content}}</div>
					</slot>
					<slot name="btn">
						<div class="btnbox">
							<div class="btn" :style="{color:cancelColor,background:cancelBackgroundColor}" @click.stop="clickBtn('cancel')">{{cancelText}}</div>
							<div class="btn" :style="{color:confirmColor,background:confirmBackgroundColor}" @click.stop="clickBtn('confirm')">{{confirmText}}</div>
						</div>
					</slot>
				</div>
			</div>
	</div>
</template>

<script>
	export default {
		name:"show-modal",
		computed: {
				show(){
					return this.$modalStore.state.show;
				},
				title(){
					return this.$modalStore.state.title;
				},
				content(){
					return this.$modalStore.state.content;
				},
				showCancel(){
					return this.$modalStore.state.showCancel;
				},
				cancelText(){
					return this.$modalStore.state.cancelText;
				},
				cancelColor(){
					return this.$modalStore.state.cancelColor;
				},
				cancelBackgroundColor(){
					return this.$modalStore.state.cancelBackgroundColor;
				},
				confirmText(){
					return this.$modalStore.state.confirmText;
				},
				confirmColor(){
					return this.$modalStore.state.confirmColor;
				},
				confirmBackgroundColor(){
					return this.$modalStore.state.confirmBackgroundColor;
				}
		},
		methods:{
			closeModal(){
				this.$modalStore.commit('hideModal')
			},
			clickBtn(res){
				this.$modalStore.commit('hideModal')
				this.$modalStore.commit('success',res)
			}
		},
		beforeDestroy(){
			this.$modalStore.commit('hideModal')
		},
		data() {
			return {
				
			};
		}
	}
</script>

<style lang="scss" scoped>
._showModal{
		position: fixed;
		top: 0;
		left:0;
		width: 100%;
		height: 100%;
		z-index:10000;
		._shade{
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			background: #000;
			opacity: .6;
			z-index:11000;
		}
		._modalBox{
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			z-index:12000;
			display: flex;
			justify-content: center;
			align-items: center;
			._modal{
				flex: none;
				width:250px;
				min-height:170px;
				background: #fff;
				border-radius: 16px;
				.title{
					text-align: center;
					font-size: 16px;
					font-family: Source Han Sans CN;
					font-weight: bold;
					color: #333333;
					margin-top: 20px;
				}
				.content{
					min-height: 80px;
					width: 86%;
					margin:20px auto;
					font-size: 20px;
					font-family: Source Han Sans CN;
					font-weight: 500;
					color: #333333;
					display: flex;
					justify-content: center;
					align-items: center;
					text-align: center;
				}
				.btnbox{
					display: flex;
					width: 88%;
					margin: auto;
					margin-bottom: 20px;
					flex-direction: row;
					justify-content: space-between;
					.btn{
						width: 100px;
						height: 32px;
						border-radius: 16px;
						display: flex;
						justify-content: center;
						align-items: center;
						font-family: Source Han Sans CN;
						font-weight: 400;
					}
				}
			}
		}
		
}
</style>

show-modal/initModal.js

import Vuex from 'vuex'
export default function initModal(v) {
  // 挂在store到全局Vue原型上
  v.prototype.$modalStore = new Vuex.Store({
    state: {
		show:false,
		title:"标题",
		content:'内容',
		showCancel:true,
		cancelText:"取消",
		cancelColor:"#ED4F4E",
		cancelBackgroundColor:"#FFE5E5",
		confirmText:"确定",
		confirmColor:"#FFFEFA",
		confirmBackgroundColor:"linear-gradient(0deg, #F65555 0%, #E54848 100%)",
		success:null,
    },
    mutations: {
		hideModal(state) {
			state.show = false
		},
		showModal(state,data) {
			state = Object.assign(state,data)
			state.show = true
		},
		success(state,res) {
			let cb = state.success
			let resObj={
				cancel:false,
				confirm:false
			}
			res=="confirm"?resObj.confirm=true:resObj.cancel=true
			cb && cb(resObj)
		}
    }
  })
  // 注册$showModal到Vue原型上,以方便全局调用
  v.prototype.$showModal = function (option) { 
	if (typeof option === 'object') {
		v.prototype.$modalStore.commit('showModal', option)
	}else{
		throw "配置项必须为对象传入的值为:"+typeof option;
	}
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3自定义组件的v-model默认绑定的是`modelValue`而不是`value`,接收的方法由`input`改为`@update:modelValue`。例如,在一个父组件,我们可以这样使用自定义组件: ``` <template> <child v-model="message" /> </template> ``` 然后在子组件,我们可以这样定义props和setup函数: ``` <script setup lang="ts"> import { defineProps, defineEmits } from 'vue' const props = defineProps({ modelValue: String }) const emits = defineEmits(['update:modelValue']) const onInput = (e) => { emits['update:modelValue'](e.target.value) } </script> <template> <input type="text" :value="modelValue" @input="onInput"> </template> ``` 通过这种方式,我们可以像使用原生的`v-model`一样在父组件使用自定义组件,并且可以正确地双向绑定`modelValue`的值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue3 自定义组件v-model](https://blog.csdn.net/weixin_46694059/article/details/128935137)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue3自定义组件使用v-model](https://blog.csdn.net/qq_42075072/article/details/123800801)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值