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');
}
相关推荐
Tiffany_Ho2 分钟前
【TypeScript】知识点梳理(三)
前端·typescript
安冬的码畜日常1 小时前
【D3.js in Action 3 精译_029】3.5 给 D3 条形图加注图表标签(上)
开发语言·前端·javascript·信息可视化·数据可视化·d3.js
太阳花ˉ1 小时前
html+css+js实现step进度条效果
javascript·css·html
小白学习日记2 小时前
【复习】HTML常用标签<table>
前端·html
john_hjy2 小时前
11. 异步编程
运维·服务器·javascript
风清扬_jd2 小时前
Chromium 中JavaScript Fetch API接口c++代码实现(二)
javascript·c++·chrome
丁总学Java2 小时前
微信小程序-npm支持-如何使用npm包
前端·微信小程序·npm·node.js
yanlele2 小时前
前瞻 - 盘点 ES2025 已经定稿的语法规范
前端·javascript·代码规范
It'sMyGo3 小时前
Javascript数组研究09_Array.prototype[Symbol.unscopables]
开发语言·javascript·原型模式