美文网首页前端大杂烩
小程序uniapp传参数

小程序uniapp传参数

作者: 写写而已 | 来源:发表于2025-01-19 15:32 被阅读0次

小程序中当需要传递大量数据参数时,可以使用消息触发或监听,注意,不适合uniapp的h5,刷新会丢失数据
例如从A页面跳转到B页面

A页面

/** 查看日志 */
function toLog() {
    uni.navigateTo({
        url: "./gemLog",
        success: res => {
            let ad = [1, 2, 3]
            let od = { key: "value" }
            res.eventChannel.emit("acceptDataFromOpenerPage", { ad, od })
        }
    })
}

B页面

import { getCurrentInstance } from "vue"

const instance = getCurrentInstance().proxy
const eventChannel = instance.getOpenerEventChannel()

eventChannel.on("acceptDataFromOpenerPage", function (data: any) {
    console.log("acceptDataFromOpenerPage", data)
})
/** 页面注销时候销毁 */
onUnload(() => {
    eventChannel.off("acceptDataFromOpenerPage")
})

如果是微信小程序,替换uni为wx

打印信息截图

相关文章

网友评论

    本文标题:小程序uniapp传参数

    本文链接:https://www.haomeiwen.com/subject/itltujtx.html