热搜:前端 nest neovim nvim

uniapp 全局函数

lxf2023-04-08 08:15:01

uniapp组件之间利用全局函数传参的方法:1、在接收参数的组件中监听全局函数;2、在传递参数的组件中注册全局函数,代码为【uni.$emit('函数名',参数)】。

uniapp 全局函数

本教程操作环境:windows7系统、uni-app2.5.1版本,Dell G3电脑。

推荐(免费):uni-app开发教程

uniapp组件之间利用全局函数传参的方法:

1、在接收参数的组件中监听全局函数

uni.$on('函数名',(形参数)=>{
...
});

2、在传递参数的组件中注册全局函数

uni.$emit('函数名',参数)

代码示例:

接收参数:

<template>
<view>meme {{this.num}}</view>
</template>
<script>
export default{
data()
{
return{
num:12
}
},
created()
{
uni.$on('update',(num)=>{
this.num=num;
});
}
}
</script>
<style>
</style>

传递参数:

<template>
<view>
<button type="primary" @click="get">按钮</button>
<me></me>
</view>
</template>
<script>
import det from '../detail/detail.vue'
import me from '../me/me.vue'
export default{
data()
{
return{
imgArr:['a'],
num2:11
}
},
components:{
det,
me
},
methods:{
get()
{
uni.$emit('update',this.num2);
}
}
}
</script>
<style scoped>
@import url("../css/a.css");
.box{
height: 375rpx;
width: 375rpx;
/* #ifdef H5 */
background-color: #4CD964;
/* #endif */
/* #ifdef APP-PLUS */
background-color: #007AFF;
/* #endif */
}
.box1{
background-color: #007AFF;
}
</style>

相关免费学习推荐:编程视频

以上就是uniapp组件之间如何利用全局函数传参的详细内容,更多请关注AdminJS其它相关文章!