// 向history推一条记录 为了用 {url:‘index.html?needClose=1‘ }这个state
window.history.pushState({ url:‘index.html?needClose=1‘ }, "index", ‘‘);
// 再添一条记录 不然获取不到第一条的state
window.history.pushState({ url:‘‘ }, "index", ‘‘);
// 定时器 刚进页面浏览器会自动push一条本页面历史记录 防止冲突
setTimeout( () => {
window.addEventListener("popstate", function (e) {
// state有值则跳转
if (e.state != null && e.state.url != ‘‘) {
location.href = e.state.url
}
});
},300 )
此时在手机detail页面点返回便可以返回到首页了
但是这时首页点返回就返回到detail页面了。。
所以首页也要改下。
获取query中参数
if( needClose ){
setTimeout( () => {
// 向history推一条记录 不然popstate事件无法触发
window.history.pushState({ url:‘‘, }, "index", ‘‘);
window.addEventListener("popstate", function (e) {
// 调用小程序api 关闭所有页面 并跳到小程序首页
wx.miniProgram.reLaunch({url: ‘/xxxxxx/xxxx})
});
},800 )
}