随着技术的不断发展,开发复杂度也越来越高。传统的开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身。为了解决这个问题,我们可以通过组件化开发来提高开发效率和降低维护成本。
本文将介绍一款快速实现前端Vue自定义弹框、自定义设置弹框内容的View popup组件,附完整源码下载地址:https://ext.dcloud.net.cn/plugin?id=12491
首先,我们需要在HTML模板中添加一个按钮元素,用于触发弹框的显示。然后,在JavaScript部分引入我们自定义的Popup组件,并通过v-if
指令控制其显示与隐藏。最后,在组件的props
中定义一些属性,如isShow
用于控制弹框是否显示、width
、height
、radius
用于设置弹框的尺寸和圆角等。
如下所示:
<template>
<div>
<button @click="showPopup">点击显示弹框button>
<cc-popup v-if="isShow" :ishide="isHide" width="calc(100vw - 70px)" height="346px" radius="16rpx">
<view class="modelContent">
<view style="margin-top: 6px;">{{ title }}view>
<view style="margin-top: 20px; color: #666666; margin: 6px 12px;">{{ content }}view>
<image class="imageV">image>
<button style="width: 80%; height: 48px;margin-top: 20px; background-color: seagreen;color: white;">确定button>
view>
<view class="close" @click="hidePopup">✕view>
cc-popup>
div>
template>
export default {
data() {
return {
title: '弹框标题',
content: '这是弹框的内容',
isShow: false,
isHide: false, // 点击其他区域时隐藏弹框
};
},
methods: {
showPopup() {
this.isShow = true;
},
hidePopup() {
this.isHide = true;
},
},
};
<template>
<view class="content">
<image class="logo" src="/static/logo.png">image>
<view class="logo" style="background-color: aquamarine;line-height: 100px;text-align: center;" @click="popupClick">点击弹框view>
<ccPopup :ishide='isshow' width="calc(100vw - 70px)" height="346px" radius="16rpx">
<view class="modelContent">
<view style="margin-top: 6px;">
弹框标题
view>
<view style="margin-top: 20px; color: #666666; margin: 6px 12px;">
这是弹框内容 这是弹框内容 这是弹框内容 这是弹框内容
view>
<image class="imageV" :src="mySrc" >image>
view>
<view class="close" @click="isshow=false">✕view>
ccPopup>
view>
template>
<script>
import ccPopup from '@/components/cc-popup.vue';
export default {
components: {
ccPopup
},
data() {
return {
title: 'Hello',
companyList:[{},{},{}],
isshow:false,
mySrc:'../../static/logo.png'
}
},
onLoad() {
},
methods: {
popupClick(){
this.isshow = !this.isshow;
}
}
}
</script>