跳转页面时调用historyState函数添加历史纪录,_state参数为每个页面的自定义参数。
historyState(_state){
let state = {
title: _state,
url: "__SELF__"
};
window.history.pushState(state, state.title, state.url);
that.historyStateCode.push(_state);
},
页面返回时进行监听。
window.addEventListener("popstate", function(e) {
//todo
});
具体到项目进行操作:
window.addEventListener("popstate", function(e) {
let et = that.historyStateCode;
if(et.length != 0){
let et2 = et[et.length-1];
eval( ' ( that.page.'+et2+'.show=false) ' );
that.historyStateCode.splice(that.historyStateCode.length-1,1)
}
});