uni-app plus.runtime.arguments 获取参数

发布于:2024-09-18 ⋅ 阅读:(123) ⋅ 点赞:(0)

uni-app plus.runtime.arguments 获取参数

问题
在 app.vue 中 onShow 获取 应用启动的参数:plus.runtime.arguments 这个是可行的. APP打开以后, 进入到了后台 然后再恢复到前台 仍然会走一次 onShow的生命周期 然后获取到plus.runtime.arguments 中的参数, 那么这参数就是重复了。 相当于还会走一次 plus.runtime.arguments 获取到的参数的逻辑 这样肯定是不行的。

-------解决办法------

在app.vue 中onLaunch 调用

this.checkArguments(); // 检测启动参数  
	// 重点是以下: 一定要监听后台恢复 !一定要  
	plus.globalEvent.addEventListener('newintent', (e) => {
		this.checkArguments(); // 检测启动参数  
	});

在 app.vue 中 methods 插入代码:

checkArguments() {  
    console.log('Shortcut-plus.runtime.launcher:启动类型: ' + plus.runtime.launcher);  
    if (plus.runtime.launcher == 'scheme') {  
        try {  
           var cmd = JSON.parse(plus.runtime.arguments);  
          //处理自己逻辑
          .............
          ............  
        } catch (e) {  
            console.log('Shortcut-exception: ' + e);  
        }  
    }  
 }

plus.runtime.arguments 参数清空

// 取到参数值后,直接赋值清空,有些情况下,单独一个赋值并不能清空,请直接使用下面两条语句。
plus.runtime.arguments = null;
plus.runtime.arguments = "";

!!! 重点 H5+ APP启动类型

可取以下值:

default”:默认启动方式,通常表示应用列表启动(360手助中搜索启动);
  “scheme”:通过urlscheme方式触发启动; 
 “push”:通过点击系统通知方式触发启动
 “stream”:通过流应用api(plus.stream.open)启动;
 “shortcut”:通过快捷方式启动,iOS平台表示通过3D Touch快捷方式,Android平台表示通过桌面快捷方式启动;
  “barcode”:通过二维码扫描启动; 
  “myapp”:通过应用收藏列表([流应用]独立App中”我的”列表)触发启动。