Ionic使用local notification时TypeScript报Expected 1 arguments错误如何解决
错误原因
当前你使用的Ionic本地通知插件版本的click事件回调仅支持1个入参,代码中传入了notification和status两个参数,触发参数数量不匹配报错。
解决方案
方案1:适配高版本插件参数规则(推荐)
目前@ionic-native/local-notifications 5.x及以上版本的click事件回调仅接收1个通知对象,原status参数已经整合到这个通知对象的属性中,你只需要调整回调写法,从通知对象里提取对应状态字段即可:
- 把回调的入参改为仅保留
notification - 打印
notification对象,确认原status对应的字段名,不同版本可能对应status、foreground等属性 - 替换原代码中
status变量的取值来源
修改后的完整代码示例:
this.lnotification.on("click", (notification)=>{ // 请根据实际打印的对象属性调整status的取值 const status = notification.status ?? notification.foreground; let ttsoptions: TTSOptions = { "text": JSON.parse(notification.data).AlarmText, "locale": 'en-US', "rate": 0.8 }; this.tts.speak(ttsoptions) .then(()=>{ console.log("it spoke tts worked"); }) .catch((ttserr)=>{ alert(JSON.stringify(ttserr) +" jp"+JSON.parse(notification.data).AlarmText+" "+ notification.data + "status is"+status+ "didnt speak"); }); this.rootPage = "AlarmlistPage"; });
如果同时存在notification为any类型的警告,可以导入插件的LocalNotification类型,给参数加上类型标注:(notification: LocalNotification) => {即可。
方案2:降级插件版本
如果你要保留原有双参数的写法,可以将本地通知插件降级到4.x版本,4.x版本的click事件回调支持2个入参,版本匹配后即可消除报错。
内容的提问来源于stack exchange,提问作者Fagbemi Ayodele
相关产品推荐
相关产品推荐

