小程序onShow事件获取options方法

微信小程序 onShow() 事件

onShow() 事件不接受参数,因此无法获取页面 url 传递过来的参数,只有 onLoad() 事件可以。

onShow(options){
  console.log(options)	//打印值为 undefined
}
onLoad(options){
  console.log(options)	//正常打印出 options 值
}

解决方法——通过小程序页面栈获取

思路:

1.获取当前小程序的页面栈—数组 长度最大是10个页面。

2.数组中索引最大的页面就是当前页面,其中可以获取到 options 属性。

onShow() {
  // 获取当前小程序的页面栈
  let pages = getCurrentPages();
  // 数组中索引最大的页面--当前页面
  let currentPage = pages[pages.length-1];
  // 打印出当前页面中的 options
  console.log(currentPage.options)		//正常打印出 options 值
},
完结~
原文地址:https://www.cnblogs.com/lwlblog/p/12346267.html