uni-app plus.runtime.arguments重复获取不到值的问题
uni-app plus.runtime.arguments重复获取不到值的问题
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | 7 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
手机系统:iOS
手机系统版本号:IOS 14
手机厂商:苹果
手机机型:iphone12
页面类型:vue
打包方式:云端
App下载地址或H5网址:
[https://apps.apple.com/cn/app/voucherspanda%E7%86%8A%E7%8C%AB%E5%9B%A2%E8%B4%AD/id1550933969](https://apps.apple.com/cn/app/voucherspanda%E7%86%8A%E7%8C%AB%E5%9B%A2%E8%B4%AD/id1550933969)
### 示例代码:
```javascript
//App.vue里面
onShow: function() {
// #ifdef APP-PLUS
//h5打开app
var args = plus.runtime.arguments;
if(!args) {
args = ''
}
console.log('args', args)
let _inviteCode = args.split('latipayW000045156://')[1]
if(!_inviteCode) {
_inviteCode = args.split('latipayw000045156://')[1]
}
console.log('_inviteCode', _inviteCode)
if (args && _inviteCode) {
let obj = {}
if (this.getReqParam(_inviteCode, 'city')) {
obj.city = decodeURIComponent(this.getReqParam(_inviteCode, 'city'))
}
if (this.getReqParam(_inviteCode, 'type')) {
obj.type = this.getReqParam(_inviteCode, 'type')
}
if (this.getReqParam(_inviteCode, 'uniqueId')) {
obj.uniqueId = this.getReqParam(_inviteCode, 'uniqueId')
}
if (this.getReqParam(_inviteCode, 'shopUniqueId')) {
obj.shopUniqueId = this.getReqParam(_inviteCode, 'shopUniqueId')
}
uni.setStorageSync('shareObj', obj);
console.log('onShow', uni.getStorageSync('shareObj'))
plus.runtime.arguments = null
plus.runtime.arguments = ''
setTimeout(function() {
uni.switchTab({
url: '/pages/tabbar/home'
})
}, 1800);
}
}
操作步骤:
app下载地址:https://apps.apple.com/cn/app/voucherspanda熊猫团购/id1550933969
分享页地址:https://user-usa.voucherspanda.com/share/index.html?rnd=0.7547830596567109&domain=https%3A%2F%2Fuser-ca.voucherspanda.com%2Fpanda&type=1&uniqueId=7a379c162c94718e017a380c22af000c&city=温哥华
分享页点击打开app
预期结果:
每次唤起app都可以接收到plus.runtime.arguments的值
实际结果:
只能在app关闭时才能接收到plus.runtime.arguments的值,app切换到后台后再唤起app不能接收到plus.runtime.arguments的值,只能将app次切换后台再切回前台才能正常获取到plus.runtime.arguments的值
bug描述:
使用plus.runtime.arguments接收h5或者<wx-open-launch-app>标签传过来的值,第一次唤起app时参数是可以正常接收的,如果这个时候把app切到后台,再次点击唤起app,这个时候是获取不到值的,把app切到后台再切回来才可以接收到参数
更多关于uni-app plus.runtime.arguments重复获取不到值的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
求助求助
更多关于uni-app plus.runtime.arguments重复获取不到值的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
plus.runtime.arguments 是异步返回的,在onShow的时机值可能还没同步,加个延时 500ms 获取一下试试看
这是一个典型的 plus.runtime.arguments 生命周期管理问题。你的代码逻辑存在缺陷,导致参数被错误地清除了。
核心问题在于:你将 plus.runtime.arguments 在第一次获取后置空了。
在你的 onShow 函数中,执行了这两行代码:
plus.runtime.arguments = null
plus.runtime.arguments = ''
这导致了以下问题:
- 第一次从 H5 页面唤起 App 时,
plus.runtime.arguments包含正确的 URL 参数。 - 你获取并处理后,将其设置为
null和''。 - 当 App 切换到后台,再次从 H5 页面唤起时,系统确实会重新设置
plus.runtime.arguments。 - 但由于你的
onShow会再次执行,此时args变量获取到的是你之前清空后的值(''),而不是系统新传入的值。
解决方案:
不要手动清除 plus.runtime.arguments。系统会自动管理这个属性。正确的做法是:
- 移除清空参数的代码:
// 删除这两行
// plus.runtime.arguments = null
// plus.runtime.arguments = ''
- 添加状态标记,避免重复处理相同的唤起请求:
let hasProcessedArguments = false
onShow: function() {
// #ifdef APP-PLUS
const args = plus.runtime.arguments
// 检查是否已经处理过当前参数
if (!args || hasProcessedArguments) {
return
}
console.log('args', args)
// 处理参数逻辑...
// 标记已处理
hasProcessedArguments = true
// 可选:延迟重置标记,避免影响下一次真正的唤起
setTimeout(() => {
hasProcessedArguments = false
}, 3000)
// #endif
}
- 或者使用更精确的判断逻辑:
onShow: function() {
// #ifdef APP-PLUS
const args = plus.runtime.arguments
// 只有当参数包含你的特定 scheme 时才处理
if (args && (args.includes('latipayW000045156://') || args.includes('latipayw000045156://'))) {
console.log('args', args)
// 处理参数逻辑...
}
// #endif
}

