uni-app 组件的通讯

父组件给子组件传值

父组件
子组件
  • 父组件绑定data里面的属性 v-bind绑定 放置test组件中
  • test组件,下面props接收,然后view里面 然后 这是父组件传递过来的数据{{title}} 显示
<template>
	<view id="myView">
		这是test组件{{num}}
		这是父组件传递过来的数据{{title}}
	</view>
</template>

<script>
	export default {
		name:"test",
		data() {
			return {
				num: 3,
				intId: null
			};
		},
		props:['title'],
		// 实例初始化,但是此时数据并没有初始化
     	beforeCreate(){
		 console.log("实例已经初始化了")
		 console.log(this.num)
		 
	    },
		// 此时数据初始化,通常在这里面显示一些数据,初始化数据在这里面
		created(){
		 console.log('created',this.num)
		 // 设置一个定时器,时间为一秒
		 this.intId= setInterval(()=>{
			 console.log('执行定时器')
		 },1000)
		},
		beforeMount() {
		 console.log('beforemount',document.getElementById('myView'))
		},
		// 操作Dom元素,推荐在这里面操作
		// 挂在需要id,根据id获取Dom元素
		mounted() {
		 console.log('beforemount',document.getElementById('myView'))
		},
		destroyed() {
			console.log("组件销毁了")
			// 通过变量销毁定时器
			clearInterval(this.intId)
		}
	}
</script>

<style>

</style>

子组件给父组件传值

通过$emit触发事件进行传递参数


<template>
	<view id="myView">
		这是test组件{{num}}
		这是父组件传递过来的数据{{title}}
		<button @click="sendNum">给父组件传值</button>
	</view>
</template>

<script>
	export default {
		name:"test",
		data() {
			return {
				num: 3,
				intId: null
			};
		},
		props:['title'],
		// 实例初始化,但是此时数据并没有初始化
     	beforeCreate(){
		 console.log("实例已经初始化了")
		 console.log(this.num)
		 
	    },
		// 此时数据初始化,通常在这里面显示一些数据,初始化数据在这里面
		created(){
		 console.log('created',this.num)
		 // 设置一个定时器,时间为一秒
		 // this.intId= setInterval(()=>{
			//  console.log('执行定时器')
		 // },1000)
		},
		beforeMount() {
		 // console.log('beforemount',document.getElementById('myView'))
		},
		// 操作Dom元素,推荐在这里面操作
		// 挂在需要id,根据id获取Dom元素
		mounted() {
		 // console.log('beforemount',document.getElementById('myView'))
		},
		destroyed() {
			console.log("组件销毁了")
			// 通过变量销毁定时器
			clearInterval(this.intId)
		},
		methods:{
			sendNum(){
				console.log('给父亲传值')
				this.$emit('myEven',this.num)
			}
		}
	}
</script>

<style>

</style>

  • 首先子组件定义一个button按钮,写一个@click事件,事件里面this.$emit(‘myEven’,this.num)
  • 在父组件里面的view的子组件里面@myEven=“getNum”,传递的值赋给当前的组件

兄弟组件之间的通信

在这里插入图片描述
在这里插入图片描述

  • 首先uni.on注册一个事件,然后在另一个组件里面uni.$emit实现整个方式

注意:在Created里面写uni. o n , 在 m e t h o d s 里 面 写 u n i . on,在methods里面写uni. onmethodsuni.emit

  • a组件
<template>
	<view>
		这是a组件:<button @click="addNum">修改b组件的数据</button>
	</view>
</template>

<script>
	export default {
		name:"a",
		data() {
			return {
				
			};
		},
		methods:{
			addNum(){
			   uni.$emit('updateNum',10)
			}	
		}	
	}
</script>

<style>

</style>

  • b组件
<template>
	<view>
		这是b组件的数据:{{num}}
	</view>
</template>

<script>
	export default {
		name:"b",
		data() {
			return {
				num:0
			};
		},
		created() {
			uni.$on("updateNum",num =>{
				this.num+=num;
			}
			)
		}
	}
</script>

<style>

</style>

  • index.vue (显示用的)
<template>
	<view class="content">
		<!-- v-if 来判断显示还是不显示 -->
		<test v-if="flag" :title="title" @myEven="getNum"></test>
		<button type="primary" @click="checkTest">切换方法</button>
		这是子组件传递过来的数据{{num}}
		<test-a></test-a>
		<test-b></test-b>
	</view>
</template>

<script>
	import test from '../../components/test.vue'
	import testa from '../../components/a.vue'
	import testb from '../../components/b.vue'
	export default {
		data() {
			return {
				title: 'Hello',
				flag: true,
				num: 0
			}
		},
		onLoad(options) {
			console.log("页面加载了",options)
		},
		onShow(){
			console.log('页面显示了')
		},
		onReady(){
			console.log('页面初次渲染完成了')
		},
		onHide(){
			console.log("页面隐藏了")
			
		},
		methods: {
			checkTest(){
				this.flag= !this.flag;
			},
			getNum(num){
				console.log(num)
				this.num = num
			}	
		},
		// 注册组件
		components:{
			test,
			"test-a": testa,
			"test-b": testb
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

在这里插入图片描述

您可以使用uni-appuni-download组件来实现下载图片功能。uni-download组件是下载文件的通用组件,可以下载任何类型的文件,包括图片文件。以下是一个简单的示例,演示如何使用uni-download组件来下载图片: ```html <template> <view> <image :src="imgUrl"></image> <button @click="downloadImg">下载图片</button> </view> </template> <script> export default { data() { return { imgUrl: 'https://example.com/image.png' } }, methods: { downloadImg() { uni.downloadFile({ url: this.imgUrl, success: (res) => { if (res.statusCode === 200) { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: () => { uni.showToast({ title: '保存成功' }) }, fail: () => { uni.showToast({ title: '保存失败', icon: 'none' }) } }) } else { uni.showToast({ title: '下载失败', icon: 'none' }) } }, fail: () => { uni.showToast({ title: '下载失败', icon: 'none' }) } }) } } } </script> ``` 在上述代码中,我们首先在data中声明了一个imgUrl变量,用于存储图片的地址。然后,在页面中展示该图片,并在按钮的点击事件中调用downloadImg方法来触发下载操作。在downloadImg方法中,我们使用uni.downloadFile来下载图片,并在下载完成后使用uni.saveImageToPhotosAlbum来保存图片到相册中。 请注意,为了使uni.downloadFile能够下载图片文件,必须在manifest.json文件中配置网络权限。您可以在manifest.json中添加以下代码: ```json { "permission": { "scope.userLocation": { "desc": "获取用户位置信息" }, "scope.writePhotosAlbum": { "desc": "保存图片到相册" }, "scope.record": { "desc": "麦克风" }, "scope.camera": { "desc": "摄像头" }, "scope.album": { "desc": "相册" }, "scope.userInfo": { "desc": "用户信息" }, "scope.userLocationBackground": { "desc": "后台定位" }, "scope.werun": { "desc": "微信运动步数" }, "scope.writeVideo": { "desc": "保存视频到相册" }, "scope.cameraRecord": { "desc": "拍摄小视频" }, "scope.invoice": { "desc": "获取发票" }, "scope.invoiceTitle": { "desc": "获取发票抬头" }, "scope.address": { "desc": "获取通讯地址" }, "scope.recordPreset": { "desc": "前置摄像头" }, "scope.writeContacts": { "desc": "通讯录" }, "scope.readSMS": { "desc": "短信" }, "scope.sensor": { "desc": "传感器" }, "scope.location": { "desc": "地理位置" }, "scope.phone": { "desc": "拨打电话" }, "scope.microphone": { "desc": "麦克风" }, "scope.notification": { "desc": "通知" }, "scope.cameraAlbum": { "desc": "拍照" }, "scope.calendar": { "desc": "日历" }, "scope.appointment": { "desc": "约会" }, "scope.reminder": { "desc": "提醒" }, "scope.bluetooth": { "desc": "蓝牙" }, "scope.chooseAddress": { "desc": "选择收货地址" }, "scope.addressBook": { "desc": "通讯录" }, "scope.locationBackground": { "desc": "后台定位" } } } ``` 在上述代码中,我们添加了"scope.writePhotosAlbum"权限,以允许应用程序保存图片到用户的相册中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值