You need to enable JavaScript to run this app. Ionic使用local notification时TypeScript报Expected 1 arguments错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Ionic使用local notification时TypeScript报Expected 1 arguments错误如何解决

错误原因

当前你使用的Ionic本地通知插件版本的click事件回调仅支持1个入参,代码中传入了notification和status两个参数,触发参数数量不匹配报错。

解决方案

方案1:适配高版本插件参数规则(推荐)

目前@ionic-native/local-notifications 5.x及以上版本的click事件回调仅接收1个通知对象,原status参数已经整合到这个通知对象的属性中,你只需要调整回调写法,从通知对象里提取对应状态字段即可:

  1. 把回调的入参改为仅保留notification
  2. 打印notification对象,确认原status对应的字段名,不同版本可能对应status、foreground等属性
  3. 替换原代码中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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 21:45:01