vue中实现缓存页面,ajax更新部分内容

举个例子项目中,这9张图与抽奖次数全是后台ajax请求来的,但是每次进页面的时候我就只要检测抽奖次数是否改变就够了。
在这里插入图片描述

首先要用keep-alive缓存当前页面。
注意:页面第一次进入,钩子的触发顺序created-> mounted-> activated,退出时触发deactivated。当再次进入(前进或者后退)时,只触发activated。
在这里插入图片描述
在这里插入图片描述
然后在页面中写activated函数与created中的方法一致,获取抽奖次数的接口加个参数。
在这里插入图片描述
最后在ajax请求中,判断如果是此参数的,就只更新次数,防止图片一直刷新

 getshuju:function(type){
  	  	            var that=this;
					this.$http.post(this.GLOBAL.host+'/draw/rewards', {
						   openId:sessionStorage.getItem("kbj_openId")
						})
						.then(function (res) {
						    if(res.data.code==0){
								 if(type=='act'){   //如果是缓存的页面只更新次数
									that.clickcount=res.data.count; 
								 }else{
									that.banks=res.data.banks;
									that.banklist2=res.data.recommendBanks;
									that.prizes=res.data.prizes;
									that.clickcount=res.data.count; 
								 }
						    }else{
						    	Toast({
						    			message:"获取数据失败",
						    			position: 'bottom',
						    			duration: 1000
						    	});
						    }
						})
						.catch(function (error) {
						     Toast({
						     		message:"请检查网络连接",
						     		position: 'bottom',
						     		duration: 1000
						     });
					});
  	  },
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值