uniapp之页面返回并调用返回页方法

在H5页面我们只需下面这样,但是打包成APP是无效的app需要单独加上一个.$vm

js 复制代码
const currentPages = getCurrentPages();
const currentPage = currentPages[currentPages.length - 1];
currentPage[funName] && currentPage[funName]();

最终代码,细心的朋友会发现,下面获取的currentPage,H5和APP是不同的,这是因为app端是先改变路由再跳转页面,而H5端是先跳转页面再改变路由(CurrentPages喻为路由)

js 复制代码
/**
 * 返回上一页且调用上一页方法
 * @param {Object} funName 方法名
 */
export function back(funName) {
	uni.navigateBack({
	  success: () => {
	    const currentPages = getCurrentPages();
	    if (currentPages.length >= 2) {
		  // #ifdef APP
		  // app 端是先改变路由再跳转页面
	      const currentPage1 = currentPages[currentPages.length - 1];
		  currentPage1.$vm[funName] && currentPage1.$vm[funName]();
		  // #endif
		  // #ifdef H5
		  // H5 端是先跳转页面再改变路由
	      const currentPage2 = currentPages[currentPages.length - 2];
		  currentPage2[funName] && currentPage2[funName]();
		  // #endif
	    }
	  }
	});
}

/**
 * 返回上一页且刷新(上一页有pullDownRefresh方法时)
 */
export function backRefresh() {
	back('pullDownRefresh');
}
相关推荐
Zhencode13 分钟前
Vue3核心运行时之runtime-core
前端·javascript·vue.js
木斯佳17 分钟前
前端八股文面经大全:腾讯WXG技术架构前端面试(2025-11-19)·面经深度解析
前端·面试·架构
感性的程序员小王27 分钟前
HTTPS页面请求HTTP接口失败?一文讲透Mixed Content
前端·后端
用户600071819101 小时前
【翻译】我竟渐渐迷上了生成器的设计巧思
前端
随逸1771 小时前
《吃透防抖与节流:从原理到实战,彻底解决高频事件性能问题》
javascript
Wect1 小时前
LeetCode 104. 二叉树的最大深度:解题思路+代码解析
前端·算法·typescript
千寻girling1 小时前
面试官 : “ 请说一下 JS 的常见的数组 和 字符串方法有哪些 ? ”
前端·javascript·面试
Wect1 小时前
LeetCode 100. 相同的树:两种解法(递归+迭代)详解
前端·算法·typescript
不会敲代码11 小时前
面试必考:如何优雅地将列表转换为树形结构?
javascript·算法·面试
用户5757303346241 小时前
深入理解 Promise:从单线程到异步流程控制的终极指南
javascript