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');
}
相关推荐
JavaGuide12 小时前
利用元旦假期,我开源了一个大模型智能面试平台+知识库!
前端·后端
yuanyxh13 小时前
程序设计
前端·设计
坚持就完事了13 小时前
JavaScript
开发语言·javascript·ecmascript
eason_fan13 小时前
前端性能优化利器:LitePage 轻量级全页设计解析
前端·性能优化·前端工程化
pe7er14 小时前
如何阅读英文文档
java·前端·后端
先做个垃圾出来………15 小时前
Python位运算及操作
java·前端·python
daqinzl15 小时前
JavaScript连接WebSocket
javascript·websocket
梦帮科技15 小时前
第三十四篇:开源社区运营:GitHub Stars增长策略
开发语言·前端·爬虫·python·docker·架构·html
POLITE315 小时前
Leetcode 19. 删除链表的倒数第 N 个结点 JavaScript (Day 11)
javascript·leetcode·链表
time_rg15 小时前
react fiber与事件循环
前端·react.js