uniapp 开发必备:复制功能实现教程(h5)

前言

当我们在使用手机或电脑上的应用程序时,复制功能是一个非常常见且必要的功能。然而,在开发移动应用程序时,实现复制功能可能会变得有点棘手。幸运的是,uniapp 为我们提供了一种相对简单的方法来实现它。在本文中,我们将介绍如何使用 uniapp 实现复制功能,以便大家可以在应用程序中轻松地添加这个重要的功能。


实现效果

在这里插入图片描述


1、安装 vue-clipboard2 插件(非h5也可以调用官方API实现)

npm install --save vue-clipboard2

2、在 main.js 中引入并挂载

// 复制
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

3、组件中调用

3.1 在 data 中定义数据

data() {
		return {
			value: "7879" //要复制的数据
		}
	},

3.2 在页面上绑定 data 中的数据

//value就是data中的value;this.value就是拿到data中value的值; @tap="copy" 点击事件
<text :value="value">{{this.value}}</text>
<text @tap="copy">复制</text>

3.3 methods 中触发点击事件

methods: {
	// 复制文本
	copy() {
		this.$copyText(this.value).then(
			res => {
				uni.showToast({
					title: '复制成功',
					icon: 'none'
				})
			}
		)
	},

完整代码:

<template>
	<view>
		<!-- 背景图 -->
		<view class="bgdImg"></view>
		<view class="titleImg">
			<image src="https://cdnapp.jixianzhilu.cn/circle_image/2021_06_1_13_28_10_1.png" />
		</view>
		<!-- 上下图文 -->
		<view class="boxCenter">
			<view class="boxContant">
				<view class="topImg">
					<image src="https://cdnapp.jixianzhilu.cn/circle_image/2021_06_1_13_45_31_9.png" mode=""></image>
					<text>•••</text>
					<image src="https://cdnapp.jixianzhilu.cn/circle_image/2021_06_1_13_45_42_2.png" mode=""></image>
					<text>•••</text>
					<image src="https://cdnapp.jixianzhilu.cn/circle_image/2021_06_1_13_46_1_4.png" mode=""></image>
				</view>
				<view class="bottomtxt">
					<view>点击"立即邀请"分享链接给好友</view>
					<view>好友点击链接下载并注册</view>
					<view>好友在APP中 登录,输入邀请码</view>
				</view>
			</view>
		</view>
		<!-- 邀请码框 -->
		<view class="bottomBtn">
			<view class="codeInp">
				<text>我的邀请码:</text>
				<text :value="value">{{this.value}}</text>
				<text @tap="copy">复制</text>
			</view>
			<view class="btnLast" @click="invite">
				立即邀请
			</view>
		</view>
	</view>

</template>
<script>
	export default {
		data() {
			return {
				value: "7879" //邀请码
			}
		},
		methods: {
			// 复制
			copy() {
				this.$copyText(this.value).then(
					res => {
						uni.showToast({
							title: '复制成功',
							icon: 'none'
						})
					}
				)
			},
			// 立即邀请
			invite() {
				console.log("立即邀请")
			}
		}
	}
</script>

<style>
	.bgdImg {
		background: url("https://cdnapp.jixianzhilu.cn/circle_image/2021_06_1_13_23_54_4.png");
		background-size: 100% 100%;
		height: 100%;
		position: fixed;
		width: 100%;
	}

	.titleImg {
		display: flex;
		justify-content: center;
		padding-top: 148rpx;
	}

	.titleImg image {
		width: 554rpx;
		height: 238rpx;
	}

	.boxCenter {
		display: flex;
		justify-content: center;
	}

	.boxContant {
		width: 688rpx;
		height: 250rpx;
		position: absolute;
		z-index: 10;
		background: #FDF3C7;
		border-radius: 22rpx;
		margin-top: 244rpx;
	}

	.topImg {
		display: flex;
		align-items: center;
		padding: 44rpx 88rpx 0rpx 88rpx;
		justify-content: space-between;
	}

	.topImg image {
		width: 60rpx;
		height: 80rpx;
		vertical-align: middle;
	}

	.topImg text {
		color: #FED10A;
	}

	.bottomtxt {
		display: flex;
		padding: 14rpx 28rpx 0rpx 28rpx;
		justify-content: space-between;
	}

	.bottomtxt view {
		width: 200rpx;
		height: 68rpx;
		font-size: 22rpx;
		font-family: "PingFangSC-Regular, PingFang SC";
		color: #2B2C2D;
		line-height: 38rpx;
		text-align: center;
	}

	.bottomBtn {
		display: flex;
		justify-content: center;
	}

	.btnLast,
	.codeInp {
		width: 640rpx;
		height: 88rpx;
		border-radius: 46rpx;
		text-align: center;
		line-height: 88rpx;
		position: absolute;
		font-family: "PingFangSC-Regular, PingFang SC";
	}

	.codeInp {
		margin-top: 534rpx;
		background: #FFFFFF;
	}

	.codeInp text {
		font-size: 28rpx;
	}

	.codeInp text:last-child {
		padding-left: 36rpx;
		color: #F7C000;
		font-weight: bold;
	}

	.btnLast {
		background-color: rgb(246, 100, 100);
		color: #FFFFFF;
		margin-top: 664rpx;
	}
</style>
  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水星记_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值