vue頁面出去時vux存頁面高度,再進來時回到原來高度
created(){
},
beforeRouteLeave(to, from, next) { //從這個頁面出去的時候執行這裡
try {
let top = document.documentElement.scrollTop || document.body.scrollTop; // 得到當前高
let scrolldata = {
url: "/Pushtwopage", //記錄哪個頁面
scrollTop: top, //記錄滾動條高度
shop: this.shop,
shopTypeName: this.shopTypeName,
};
this.$store.commit("set_scrollPostions", scrolldata); //把要存的存到set_scrollPostions裡
} catch (err) {}
next();
},
mounted() {
var url = window.location.pathname; //獲取一下頁面url的後面一截
url = "/Pushtwopage";
var scrolls = this.$store.state.scrollPostions; //把vux存的資料拿出來
var scrollTop = 0;
this.shop.data = [];
for (let i = 0; i < scrolls.length; i++) {
if (scrolls[i].url == url) {
scrollTop = scrolls[i].scrollTop;
this.shop = scrolls[i].shop;
this.shopTypeName = scrolls[i].shopTypeName;
break;
}
}
setTimeout(function() {
scroll(0, scrollTop);
}, 10);
},
//store.js檔案
import Vue from 'vue'
import Vuex from 'vuex'
import Qs from 'qs'
Vue.use(Vuex)
const state = {
user:{},
token:'',
addrdata:[],
scrollPostions:[],
triplist:[],
catchMap:new Map(),
}
const mutations = {
set_user (state, user) {
state.user = user
},
set_token (state, token) {
state.token = token
},
set_addrdata(state,data){
state.addrdata=data
},
set_tripList(state,data){
state.triplist=data
},
set_scrollPostions (state, currscrollPostion) {
for(let i=0;i<state.scrollPostions.length;i++){
if(state.scrollPostions[i].url==currscrollPostion.url){
state.scrollPostions[i]=currscrollPostion;
return;
}
}
state.scrollPostions.push(currscrollPostion);
}
}
export default new Vuex.Store({
state,
mutations
})