1)sessionStorage的用法:sessionStorage仅在当前会话下有效,关闭页面或浏览器后被清除。存放数据大小为一般为5MB,而且它仅在客户端(即浏览器)中保存,不参与和服务器的通信。源生接口可以接受,亦可再次封装来对Object和Array有更好的支持。(简言之:关闭页面就销毁)
2)localStorage的用法:
localStorage生命周期是永久,这意味着除非用户显示在浏览器提供的UI上清除localStorage信息,否则这些信息将永远存在。存放数据大小为一般为5MB,而且它仅在客户端(即浏览器)中保存,不参与和服务器的通信。(简言之:你不手动清除它就一直在。)
1、列表页
methods: {
goDetail(row) {
this.$router.push({
name: 'spuDetail',
params: {id: row.id},
});
},
}
2、详情页
beforeRouteEnter(to, from, next) {
next(vm => {
const routeParams = vm.getRouteParams();
if (routeParams.id) {
vm.getDetail(routeParams.id);
}
});
},
beforeRouteLeave (to, from, next) {
sessionStorage.removeItem('routeParams');
next();
},
methods: {
getDetail(id){
},
getRouteParams(){
const routeParams = sessionStorage.getItem('routeParams');
if (routeParams) {
return JSON.parse(routeParams);
} else {
sessionStorage.setItem('routeParams', JSON.stringify(this.$route.params));
return this.$route.params;
}
}
},
网友评论