需求:从特定页面回退或进入该页面并限制执行某个函数。
问题:由于页面有封装的topbar函数自带返回,因此很难再加标识。
方案:走栈里面抽取上一级页面进行比对限制该函数执行。
代码:
// 走栈抽取上一级页面
const currentPage = getCurrentPages().pop();
const lastPath = currentPage?.__displayReporter.showReferpagepath;
// searchPath指特定页面
const searchPath = "pages/activityEntry/activityTracks/activityTracksSearch/activityTracksSearch.html";
console.log(currentPage);
console.log(lastPath);
if( lastPath != searchPath ) {
// 获取用户足迹
this.getUserTracks();
}
打印效果:
最后判断是否是指定路径来决定是否限制该函数执行。