uni-app组件的通讯

uni-app组件的通讯

1.父组件给子组件传值

  父组件传递数据( :text动态绑定并传递属性text)给子组件,代码如下:

<template>
	<view class="">
		这是父组件页面,来自子组件的信息{{msg}}
		<son :text= "text" @update="receiveMsg"/>
	</view>
</template>

<script>
	import son from './son.vue'
	export default {
		name:'father',
		components: {
			son
		},
		data() {
			return {
				text: 'hello',
				msg:''
			}
		},
		methods:{
			receiveMsg(msg) {
				this.msg = msg
			}
		}
	}
</script>

<style>
</style>

  子组件通过props来接受外界传递到组件内部的值代码如下:

template>
	<view class="">
		这是子组件页面,来自父组件的文本:{{text}}
		<button type="primary" @click="sendMsg">点击子组件传递信息给父组件</button>
	</view>
</template>

<script>
	export default {
		name:'son',
		
		props: {
			text: {
				type: String,
				default:''
			}
		},
		data() {
			return {
			}
		},
		methods:{
			sendMsg() {
					this.$emit("update", 'world');
			}
		}
	}
</script>

<style>
</style>
2.子组件给父组件传值

  通过$emit触发事件进行传递参数
  子组件代码:

<template>
	<view class="">
		这是子组件页面,来自父组件的文本:{{text}}
		<button type="primary" @click="sendMsg">点击子组件传递信息给父组件</button>
	</view>
</template>

<script>
	export default {
		name:'son',
		
		props: {
			text: {
				type: String,
				default:''
			}
		},
		data() {
			return {
			}
		},
		methods:{
			sendMsg() {
					this.$emit("update", 'world');
			}
		}
	}
</script>

<style>
</style>

  父组件通过@监听自定义事件update,代码如下:

<template>
   <view class="">
   	这是父组件页面,来自子组件的信息{{msg}}
   	<son :text= "text" @update="receiveMsg"/>
   </view>
</template>

<script>
   import son from './son.vue'
   export default {
   	name:'father',
   	components: {
   		son
   	},
   	data() {
   		return {
   			text: 'hello',
   			msg:''
   		}
   	},
   	methods:{
   		receiveMsg(msg) {
   			this.msg = msg
   		}
   	}
   }
</script>

<style>
</style>

2.兄弟组件通讯

  需要借助uni.$emituni.$onuni.$offuni.$once,这四者常用于跨页面、跨组件通讯
  1).先监听页面全局的自定义事件。事件可以由 uni.$emit 触发,回调函数会接收所有传入事件触发函数的额外参数

<template>
	<view>
		这是b组件:{{num}}
	</view>
</template>

<script>
	export default {
		name:"TestB",
		data() {
			return {
				num: 0
			};
		},
		onLoad() {
			console.log('onLoad');
			// 测试组件有没有回调onLoad
		},
	  created() {
			console.log('created');
			uni.$on("update",(num)=>{
				this.num += num;
				console.log('来自a组件的数据', num)
			})
		},
		destroyed() {
			console.log('destroyed');
			// 因为事件监听是全局的,所以使用 uni.$on ,需要使用 uni.$off 移除全局的事件监听,避免重复监听
			 uni.$off("update");
		}
	}
</script>

<style>

</style>

  2).触发事件

<template>
	<view>
		这是a组件 <button type="primary" @click="sendNum()">点击a组件传递num给b组件</button>
	</view>
</template>

<script>
	export default {
		name:"TestA",
		data() {
			return {
				
			};
		},
		methods:{
			sendNum(num) {
				setInterval(()=>{
								uni.$emit("update", 1);
				}, 2000)
	
			}
		}
	}
</script>

<style>

</style>

  3). 注意使用时,注意及时销毁事件监听,比如,页面 onLoad 里边 uni.$on注册监听,onUnload 里边 uni.$off移除,或者一次性的事件,直接使用 uni.$once监听。
  例子,未使用uni.$off 移除全局的事件监听,造成重复监听,A组件使用计时器每2秒触发事件一次,即使销毁了B组件,B组件依旧可以监听到。
在这里插入图片描述
  解决方法,就是使用uni.$off移除

	destroyed() {
   		console.log('destroyed');
   		// 因为事件监听是全局的,所以使用 uni.$on ,需要使用 uni.$off 移除全局的事件监听,避免重复监听
   		uni.$off("update");
   	}

  如果是一次性的事件,直接使用 uni.$once监听,$once(eventName,callback)监听全局的自定义事件。事件可以由 uni.$emit 触发,但是只触发一次,在第一次触发之后移除监听器

	uni.$once("update",(num)=>{
				this.num += num;
				console.log('来自a组件的数据', num)
			 })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用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、付费专栏及课程。

余额充值