Skip to content
泠泠彻夜的笔记
Main Navigation首页笔记
HTML
CSS
JavaScript
TypeScript
Node.js
Vue3
uniapp
插件
算法题

外观

blogs

可展示自定义内容的uniapp弹窗

怡然

293字小于1分钟

uniapp自用小组件

2025-06-06

  • 父组件
详情
<template>
  <view class="form-area-item">
    <text class="form-area-item-text">选择模板</text>
    <uni-data-select v-model="modelIndex" :localdata="array" @change="bindPickerChange"></uni-data-select>
    <CustomModal :visible="isModalVisible" :message="modalMessage" :imageSrc="modalImageSrc" @confirm="onConfirm"
      @cancel="onCancel" />
  </view>
</template>
<script>
export default{
  methods:{
    bindPickerChange(e) {
      const tempOption = this.array.find(item => item.value == e).text;
      this.modalMessage = `您选择的是: ${tempOption}`;
      this.modalImageSrc = '../static/images/xijiao/avator.png';
      this.isModalVisible = true;
      this.modelIndex = e
    },
  }
}
</script>
  • 自定义组件
详情
<template>
	<view class="modal" v-if="visible">
		<view class="modal-content">
			<text class="mes-text">{{ message }}</text>
			<image :src="imageSrc" class="modal-image" mode="aspectFit" />
			<view class="modal-buttons">
				<button class="login-btn cu-btn block bg-gray lg round" @click="onCancel">取消</button>
				<button class="login-btn cu-btn block bg-red lg round" @click="onConfirm">确认</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			visible: {
				type: Boolean,
				default: false
			},
			message: {
				type: String,
				default: ''
			},
			imageSrc: {
				type: String,
				default: ''
			}
		},
		methods: {
			onConfirm() {
				this.$emit('confirm');
			},
			onCancel() {
				this.$emit('cancel');
			}
		}
	};
</script>

<style>
	.modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 9999;
	}

	.modal-content {
		background: white;
		padding: 20px;
		border-radius: 10px;
		text-align: center;
		width: 250px;
		display: flex;
		align-items: center;
		flex-direction: column;
	}
	.mes-text{
		margin: 10px 0;
	}
	.modal-image {
		display: block;
		width: 150px;
		height: 150px;
	}

	.modal-buttons {
		margin-top: 20px;
		width: 100%;
		display: flex;
		justify-content: space-between;
	}
</style>
  • 效果如下:

示例图片

贡献者: tsxwslk
上一页trycatch中dialog打开失效的问题
下一页扩散圆效果

Power by VuePress & vuepress-theme-plume