微信小程序传参过来了,但是数据没有获取到
使用本方法前,已经采用encodeURIComponent把拼接的参数编码之后,拼接在链接上,在接受的页面的onLoad生命周期,接收到参数之后,采用decodeURIComponent进行解码的操作,如果这个也不行,不是说不行,而是第一次跳转没有解析出来数据,也就是页面没有数据。需要第二次跳转才有数据。那么就是在微信小程序中,URL参数传递有以下限制:
- 长度限制:URL过长会被截断
- 编码问题:特殊字符可能导致解析失败
- 生命周期问题:页面加载时序可能影响数据获取
解决方案就是只传递关键的参数。或者跳转过去再做查询功能
传递的参数设置
clickPatient(item) {if (this.patientInfo) {// 简化方案:只传递最关键的几个参数const params = {patientId: item.patientId || '',brName: item.brName || '',brSfzh: item.brSfzh || '',groupStatus: item.groupStatus || '0',companyCode: item.companyCode || '',rid: item.rid || ''};// 同时使用存储作为备份uni.setStorageSync('current_patient_backup', item);// 构建URL参数const urlParams = Object.keys(params).filter(key => params[key]) // 过滤掉空值.map(key => `${key}=${encodeURIComponent(params[key])}`).join('&');console.log('URL参数字符串:', urlParams);uni.navigateTo({url: `./patientDetail?${urlParams}`,});}},
接受参数设置
onLoad(options) {this.mrPatientInfo = this.$patientInfo.getCurrentPatient()let patientData = null;// 简化方案:直接从URL参数构建数据if (options.patientId) {patientData = {patientId: decodeURIComponent(options.patientId),brName: options.brName ? decodeURIComponent(options.brName) : '',brSfzh: options.brSfzh ? decodeURIComponent(options.brSfzh) : '',groupStatus: options.groupStatus || '0',companyCode: options.companyCode ? decodeURIComponent(options.companyCode) : '',rid: options.rid || ''};}// 备用方案:从存储获取if (!patientData || !patientData.patientId) {try {const backupData = uni.getStorageSync('current_patient_backup');if (backupData && backupData.patientId) {patientData = backupData;// 清理备份数据uni.removeStorageSync('current_patient_backup');}} catch (error) {}}