uniapp 页面之间通信

691 阅读1分钟

uniapp 中页面通信和 vue 相似,但也有不同之处。

  • uniapp 中主要使用 [uni.emit](https://uniapp.dcloud.net.cn/api/window/communication.html#) 来进行数据的发送,等同于 vue 中的 thi.emit()
  • uniapp 中使用uni.$on 来接收通信的数据

image.png

具体使用方式:

uni.$emit('update','发送的数据')
uni.$on('update',function(data){
    console.log(data);
})

uniapp 与 vue 对比

习惯了 vue 中的 emit 写法,要注意:

  • vue 是 this.$emit(),而 uniapp 使用的是 uni.
  • 接收事件不再使用 @xxx="" 的形式

注:如果是页面引入的组件文件,不要把将其放在 onLoad() 中(组件的 onLoad 不会被执行,只有页面才有 onLoad)