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');
}
相关推荐
Leo655352 分钟前
动态 SQL(行+列) + 动态表头(前端+EasyPoi) = 完整透视报表系统
前端·sql·状态模式
Gary jie3 分钟前
OpenClaw启动日志详细分析
前端·chrome
择势8 分钟前
macOS App 签名与公证流程详解及一键自动化
前端
英俊潇洒美少年20 分钟前
Vue3 中 watch的 flush 选项(默认无/`post`/`sync`)的区别
前端·javascript·vue.js
闲云一鹤22 分钟前
Python 入门(三)- PyAutoGUI 自动化教程
前端·python·黑客
D_C_tyu25 分钟前
HTML | 结合Canvas开发具有智能寻路功能的贪吃蛇小游戏实战详解
javascript·算法·游戏·html·bfs
Jay-r28 分钟前
樱花雨特效 WebGL实现 短视频同款浪漫视觉效果(附源码下载)
开发语言·javascript·ecmascript·编程·webgl·代码·樱花雨
凤山老林28 分钟前
Js如何实现一个抽奖程序
前端·javascript·vue.js
我命由我1234530 分钟前
React - Switch、路由精准匹配与模糊匹配、Redirect
开发语言·前端·javascript·react.js·前端框架·html·ecmascript
蓝色心灵-海35 分钟前
小律书 技术架构详解:前后端分离的自律管理系统设计
java·http·小程序·架构·uni-app