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');
}
相关推荐
CappuccinoRose3 分钟前
输入事件进阶
前端·javascript·输入事件
liangshanbo12152 小时前
面试题:前端工程化中 Tree Shaking的原理是什么?
前端·treeshaking
500842 小时前
React Native for OpenHarmony 实战:三方库 react-native-url-polyfill 的鸿蒙化适配指南
javascript·react native·react.js·性能优化·electron·harmonyos
补码缩写2 小时前
视频跳转触发了seeked,画面就到位了吗
前端
库拉镜像AI牛牛2 小时前
漫剧工作室量产方案:依托知漫剧 AI 短剧降本增效
大数据·服务器·前端·人工智能·语音识别
不可能片场2 小时前
内容运营自动化 用相似度拦住撞车选题
前端·electron
静默回滚2 小时前
视频进度条跳回去,先别改事件监听
前端
米多科技3 小时前
让 AI 走进我的 3D 世界:一个 MCP server 的完整实现
javascript
Lank_M3 小时前
MP4和WebM导出后为何起播与拖动不同
前端
用户5508492902563 小时前
前端本地存储怎么选:Cookie、localStorage、sessionStorage、IndexedDB 的取舍
前端