Uni-app 页面跳转传参方法详解 - 使用 uni.navigateTo、uni.redirectTo 和 uni.navigateBack 传递数据
在uni-app中,可以通过使用uni.navigateTo或uni.redirectTo方法携带数据跳转页面。\n\n1. 使用uni.navigateTo方法携带数据跳转页面:\njavascript\nuni.navigateTo({\n url: '/pages/detail/detail?id=123&name=example',\n success: function (res) {\n console.log('跳转成功');\n }\n});\n\n在目标页面的onLoad生命周期函数中,可以通过options参数获取传递的数据:\njavascript\nonLoad: function (options) {\n console.log('id:', options.id); // 输出 123\n console.log('name:', options.name); // 输出 example\n}\n\n\n2. 使用uni.redirectTo方法携带数据跳转页面:\njavascript\nuni.redirectTo({\n url: '/pages/detail/detail?id=123&name=example',\n success: function (res) {\n console.log('跳转成功');\n }\n});\n\n同样在目标页面的onLoad生命周期函数中,可以通过options参数获取传递的数据。\n\n除了以上两种方法,还可以使用uni.navigateBack方法返回上一页,并在uni.navigateBack的delta参数中指定返回的页面数,可以在返回的页面中通过getCurrentPages方法获取到上一页传递的数据。\n\n需要注意的是,以上方法只适用于跳转到非tabBar页面的情况。如果需要跳转到tabBar页面,可以使用uni.switchTab方法,并在目标页面的onShow生命周期函数中获取传递的数据。
原文地址: https://www.cveoy.top/t/topic/pxFZ 著作权归作者所有。请勿转载和采集!