uni-app TypeError: uni.removeSavedFile is not a function

uni-app TypeError: uni.removeSavedFile is not a function

信息类别 内容
产品分类 uniapp/App
PC开发环境 Mac
PC版本号 10.15.3
HBuilderX类型 正式
HBuilderX版本 3.2.12
手机系统 全部
手机厂商 华为
页面类型 vue
打包方式 云端
项目创建方式 HBuilderX

测试过的手机:

apple x | oppo

示例代码:

uni.removeSavedFile({ filePath: 'xxx' });

操作步骤:

uni.removeSavedFile({ filePath: 'xxx' });

预期结果:

能正常使用

实际结果:

17:39:57.862 TypeError: uni.removeSavedFile is not a function. (In ‘uni.removeSavedFile({ filePath: “xxx” })’, ‘uni.removeSavedFile’ is undefined) __ERROR


### bug描述:

TypeError: uni.removeSavedFile is not a function


更多关于uni-app TypeError: uni.removeSavedFile is not a function的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

别人都没遇到过吗?难道是我打开方式不对?

更多关于uni-app TypeError: uni.removeSavedFile is not a function的实战教程也可以访问 https://www.itying.com/category-93-b0.html


我也刚碰到,hbulder版本: 3.2.16.20211122,用的是vue3版本

3.2.2 的时候还能用,之后的版本都用不了。官方也没回复,这个帖子挂了好长时间了都

HBuiderX 3.4.4+ 版本 已修复bug

这是一个典型的 API 调用问题。uni.removeSavedFile 是 App 平台特有的 API,但在你的代码中,它被当作通用 API 调用。

问题原因: uni.removeSavedFile 属于 App 平台扩展 API,仅在 App 平台(iOS/Android)的 plus 环境下有效。在 H5 或小程序平台调用时,该 API 不存在,因此报错 is not a function

解决方案: 使用条件编译,确保只在 App 平台执行该代码:

// 正确写法
// #ifdef APP-PLUS
uni.removeSavedFile({ filePath: 'xxx' });
// #endif

补充说明:

  1. 如果你需要在多平台处理文件删除,应使用条件编译分别处理:

    • App 平台:uni.removeSavedFile
    • 小程序平台:使用各小程序自身的文件 API(如微信的 wx.removeSavedFile
    • H5 平台:通常无需此操作
  2. 确保文件路径正确,removeSavedFile 只能删除通过 uni.saveFile 保存的文件。

  3. 在开发阶段,建议使用 uni.getSystemInfo 判断平台,或通过 uni.canIUse 检测 API 可用性。

调试建议: 在调用前添加平台判断:

if (uni.removeSavedFile) {
    uni.removeSavedFile({ filePath: 'xxx' });
} else {
    console.log('当前平台不支持 removeSavedFile');
}
回到顶部