uniapp全局事件uni.$on,可以不相邻的组件之间传递参数

目录

uniapp全局事件,也就是说,不相邻的,不是父子组件,也可以传递参数。

一个组件,传递项目内所有文件其中一个里面内,可以接受到参数。

传送参数页面

html 复制代码
<template>
	<view class="box">
		<button type="default" @click="goChuangcan(521)"></button>
	</view>
</template>

<script>
	export default{
		methods:{
			// 传送参数
			goChuangcan(e){
				uni.$emit('citySelectData',e)
			}
		}
	}
</script>

<style>
	.box{
		background-color: pink;
	}
</style>

接受参数页面

html 复制代码
<template>
	<view class="box">
		接受到数据啦,展示在页面上啦:-------	<text style="color: #FFF;">{{dataLit}}</text>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				dataLit:''
			}
		}
		mounted() {
			// 注意这里是细节,一定要加	var that = this		这个
			var that = this
			uni.$on('citySelectData',function(data){
				that.dataLit = data
				console.log('接受到数据啦:',data)
			})
		}
	}
</script>

<style>
	.box{
		background-color: pink;
	}
</style>

最后

感觉文章好的话记得点个心心和关注和收藏,有错的地方麻烦指正一下,如果需要转载,请标明出处,多谢!!!

相关推荐
yuanyxh2 小时前
Mac 软件推荐
前端·javascript·程序员
万少2 小时前
AtomCode开发微信小程序《谁去呀》 全流程
前端·javascript·后端
某人辛木2 小时前
Web自动化测试
前端·python·pycharm·pytest
Kagol3 小时前
Superpowers GSD gstack AgentSkills深度测评
前端·人工智能
excel4 小时前
JavaScript 字符串与模板字面量:从表象到本质理解
前端
京东云开发者4 小时前
当AI成为导演-如何用AI创作动漫短剧
前端
李白的天不白5 小时前
使用 SmartAdmin 进行前后端开发
java·前端
乘风gg5 小时前
🤡PUA AI Coding 工具 的 10 条终极语录
前端·ai编程·claude
学Linux的语莫5 小时前
Vue 3 入门教程
前端·javascript·vue.js
怕浪猫5 小时前
第一章、Chrome DevTools Protocol (CDP) 详解
前端·javascript·chrome