uni-app 真机运行 getApp,getCurrentPages 无法使用,获取返回值时导致内存溢出

uni-app 真机运行 getApp,getCurrentPages 无法使用,获取返回值时导致内存溢出

示例代码:

onLoad(){
console.log('所有页面', getCurrentPages());
}

操作步骤:

直接执行示例中的代码,使用真机运行,打开调试工具控制台即可看到异常

预期结果:

正常返回页面栈

实际结果:

抛出内存溢出异常,页面变卡顿

bug描述:

getApp,getCurrentPages在真机运行不了,获取返回值会导致内存时溢出


| 项目属性         | 值           |
|------------------|--------------|
| 产品分类         | uniapp/小程序/百度 |
| PC开发环境操作系统 | Windows      |
| PC开发环境操作系统版本号 | windows10    |
| HBuilderX类型     | 正式         |
| HBuilderX版本号   | 3.8.1        |
| 第三方开发者工具版本号 | 4.33.1       |
| 基础库版本号      | 3.500.16     |
| 项目创建方式      | HBuilderX    |

![Image 1](https://www.itying.com/uniimg.php?url=https://img-cdn-tc.dcloud.net.cn/uploads/questions/20240117/3b23458711a03b01f503d00c8689b99c.png)
![Image 2](https://www.itying.com/uniimg.php?url=https://img-cdn-tc.dcloud.net.cn/uploads/questions/20240117/28b613579147d5dfdce611a29cd401e1.jpg)
![Image 3](https://www.itying.com/uniimg.php?url=https://img-cdn-tc.dcloud.net.cn/uploads/questions/20240117/6a407d87b7ce7f4c2b182f3fc2f03f21.jpg)

更多关于uni-app 真机运行 getApp,getCurrentPages 无法使用,获取返回值时导致内存溢出的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app 真机运行 getApp,getCurrentPages 无法使用,获取返回值时导致内存溢出的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在 uni-app 中,getAppgetCurrentPages 是常用的 API,用于获取应用实例和当前页面栈。如果你在真机运行时遇到 getAppgetCurrentPages 无法使用,或者获取返回值时导致内存溢出的问题,可能是由于以下原因导致的:

1. 检查 uni-app 版本

  • 确保你使用的 uni-app 版本是最新的,或者至少是一个稳定的版本。旧版本可能存在一些已知的 bug,更新到最新版本可能会解决问题。

2. 检查代码逻辑

  • 确保你在正确的时机调用 getAppgetCurrentPages。例如,getCurrentPages 只能在页面生命周期函数或页面方法中调用,不能在 App.vueonLaunch 等全局生命周期中调用。
  • 如果你在 onLoadonShow 等生命周期函数中调用这些 API,确保它们没有被频繁调用,导致内存溢出。

3. 内存溢出问题

  • 如果你在获取 getCurrentPages 的返回值时导致内存溢出,可能是因为页面栈过大,或者你在循环中频繁调用这些 API。
  • 你可以尝试减少页面栈的深度,或者在不需要的时候手动清理页面栈。

4. 调试工具

  • 使用 uni-app 提供的调试工具(如 HBuilderX 的调试功能)来检查是否有内存泄漏或其他异常情况。
  • 在真机调试时,可以使用 Chrome 的 DevTools 来监控内存使用情况,看看是否有异常的内存增长。

5. 真机环境问题

  • 有些真机环境可能存在兼容性问题,尤其是在低端设备上。你可以尝试在不同的设备上运行,看看问题是否普遍存在。
  • 如果问题只在特定设备上出现,可能是该设备的系统或浏览器内核存在兼容性问题。

6. 代码示例

  • 确保你正确使用了 getAppgetCurrentPages。以下是一个简单的示例:
// 获取应用实例
const app = getApp();

// 获取当前页面栈
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
console.log(currentPage);

7. 错误处理

  • 在调用这些 API 时,添加错误处理逻辑,避免因为异常情况导致应用崩溃。
try {
    const app = getApp();
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    console.log(currentPage);
} catch (error) {
    console.error('Error getting app or pages:', error);
}
回到顶部