简化版:实现模块添加/删除功能(uni-app-demo)

本文介绍了一种在前端页面中实现动态添加和删除组件的方法。通过点击加号按钮可以增加新的组件显示,并且每个新增的组件都具备独立的删除功能。具体实现包括使用Vue.js的v-for指令遍历组件列表,结合点击事件来更新数据状态。

一、效果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
二、描述
点击加号增加下面的盒子,点击×号删除盒子
三、思路
添加
1、给一个全局数组,保存上面添加的内容(即盒子)
2、点击加号,展示下面三个盒子的角标(小加号),并添加点击事件
3、通过下标获取点击的盒子,然后用push保存盒子在一个数组里面
删除
1、点击上面盒子的×号,获取该下标,用splice删除该下标,从而删除数据

四、代码
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
附上代码

<template>
	<view class="content">
		<!--更多 -->
		<view class="more">
			<view class="mtext">常用模块</view>
			<!-- 保存更多 -->
			<view class="cybox">
				<view class="wbox" v-for="(i,index2) in imgsrcs">
					<view v-on:click="addbox(item)" class="savebox" :src="imgsrcs">
						{{i.text}}
					</view>
					<view @click="del(index2)">
						<image class="xx" src="../../static/image/cha.png"></image>
					</view>
				</view>
				<view class="morebox" @click="seeopen()">
					<image class="moreimg" src="../../static/image/add.png"></image>
				</view>
			</view>
		</view>

		<!-- 添加模块 -->
		<view class="moudle">
			<view v-for='(m,index) in sysList' class="add">
				<view v-show="see" class="badge" @click="addbox(m)">
					<image class="Sbox" src="../../static/image/add.png"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "originality",
		components: {},
		data() {
			return {
				see: false,
				imgsrcs: [],
				sysList: [{
						text: '活动日',
						img: '../../',
					}, {

						text: '分析会',
						img: '../../../../../static/image/ph/gcjdk.png',
					},
					{
						text: '活动文件',
						img: '../../../../../static/image/ph/gcjdk.png',
					}
				]
			}
		},
		methods: {
			seeopen() {
				this.see = true
			},
			addbox(m) {
				console.log(m)
				this.imgsrcs.push(m)
			},
			del(item) {
				console.log(item)
				this.imgsrcs.splice(item, 1)
			}

		}
	}
</script>

<style scoped>
	.badge {
		width: 25rpx;
		height: 25rpx;
		position: absolute;
		border-radius: 50%;
		margin-left: 90rpx;
		margin-top: -10rpx;
	}


	.morebox {
		width: 100rpx;
		height: 100rpx;
		margin-left: 5%;
		display: flex;
		justify-content: center;
		align-items: center;
		border: 1rpx solid #F7B2B2;
	}

	.moreimg {
		width: 50rpx;
		height: 50rpx;

	}

	.more {
		width: 100%;
		height: 300rpx;
		border: 1rpx solid #000000;
	}

	.moudle {
		width: 100%;
		height: 600rpx;
		border: 1rpx solid #000066;
		display: flex;
		flex-wrap: wrap;
	}

	.add {
		width: 100rpx;
		height: 100rpx;
		border: 1rpx solid #000000;
		margin-top: 2%;
		margin-left: 5%;
	}

	.mtext {
		margin: 3%;
	}

	.Sbox {
		width: 25rpx;
		height: 25rpx;
		position: absolute;
		border-radius: 50%;
	}

	.savebox {
		width: 100rpx;
		height: 100rpx;
		margin-left: 5%;
		border: 1rpx solid #000000;
	}

	.cybox {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
	}

	.xx {
		width: 25rpx;
		height: 25rpx;
		position: absolute;
		border-radius: 50%;
		margin-left: 12%;
		margin-top: -15%;
	}

	.wbox {
		margin-left: 5%;
	}
</style>

一个小小的demo完成啦嘿嘿

01:05:26.458 [Vue warn]: Unhandled error during execution of async component loader \n at <AsyncComponentWrapper>\nat <PageBody>\nat <Page>\nat <Anonymous>\nat <KeepAlive>\nat <RouterView>\nat <Layout>\nat <App> 01:05:26.458 ‍[⁠TypeError⁠]‍ {message: "Failed to fetch dynamically imported module: http://localhost:5173/pages/index/index.vue"} <template> <view style="padding-left: 15px;padding-right: 15px;"> <view class="datarow"> <uni-list-item title="温度" :rightText="deviceData.temperature+'℃' || '加载中'" thumb="/static/temp.png" class="datacard" /> <uni-list-item title="湿度" :rightText="deviceData.humidity+'%' || '加载中'" thumb="/static/humi.png" class="datacard" /> </view> <view class="datarow"> <uni-list-item title="气压" :rightText="deviceData.Pa+'kPa' || '加载中'" thumb="/static/pressure.png" class="datacard" /> <uni-list-item title="空气质量" :rightText="deviceData.AQI || '加载中'" thumb="/static/quality.png" class="datacard" /> </view> <view class="datarow"> <uni-list-item title="CO2" :rightText="deviceData.ECO2+'ppm' || '加载中'" thumb="/static/CO2.png" class="datacard" /> <uni-list-item title="光照" :rightText="deviceData.light+'lx' || '加载中'" thumb="/static/illumin.png" class="datacard" /> </view> <view class="datarow"> <uni-list-item title="水位值" :rightText="deviceDataTank.waterlevel || '加载中'" thumb="/static/waterlevel.png" class="datacard" /> <uni-list-item title="雨量值" :rightText="deviceDataTank.rainfall || '加载中'" thumb="/static/rainfall.png" class="datacard" /> </view> <view class="datarow"> <uni-list-item title="蜂鸣" thumb="/static/buzzer.png" class="datacard"> <template v-slot:footer> <view class="dataslot"> <switch :checked="buzzer" @change="onSwitchChange($event, 'buzzer')" color="#2b9939" /> </view> </template> </uni-list-item> <uni-list-item title="制冷" thumb="/static/cold.png" class="datacard"> <template v-slot:footer> <view class="dataslot">
03-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值