 阅读本文约需要5分钟
阅读本文约需要5分钟大家好,我是你们的导师,我每天都会在这里给大家分享一些干货内容(当然了,周末也要允许老师休息一下哈)。上次老师跟大家分享了VUE 之 v-on指令的知识,今天跟大家分享下Vue之 点击返回弹出推荐商品弹窗的知识。
1 需求场景非推荐商品详情页返回的时候弹出弹窗推荐商品,点击弹窗按钮可以直接访问推荐商品;只有直接进入商品详情页返回才会弹出推荐商品弹窗;每个用户访问只能弹一次(除非清除缓存)。2 需求分析1. 判断是否直接进入商品详情页可以在商品详情页直接判断 history.length ,如果是则 history.length=2 ;2. 每个用户只能弹一次可以保存一个标志值 hasBeenRecommended=true 到缓存中,有了这个标志值就不再弹出;3. 返回的监控,还是要监听页面的popstate,如果要阻止原来的返回操作的话,就要给页面栈添加一个空栈:history.pushState(null, null, document.URL);......//修改历史记录状态,需要放在商品数据初始化之后if(history.length<=2 && !localStorage.getItem("hasBeenRecommended")) {    //获取推荐数据信息,页面返回标识修改及页面栈加入空栈操作在判断非当前页面之后再进行                                        this.getRecommendGoodInfo();}....../**             * 获取推荐权益卡信息数据     */        getRecommendGoodInfo: function() {            var self = this            self.$ajax({                method: 'post',                url: self.$utils.DOMIN + 'goods/getAdvert',            }).then((response) => {                var data = response.data.d                if(data && data.goodsId != self.$route.query.goodsId) {                    //给页面添加空的页面栈,同时给推荐弹窗填充数据                    history.pushState(null, null, document.URL);                    //保存阻断返回的标志到缓存中            localStorage.setItem('stopBack',true);                    //填充弹窗元素数据源                    self.recommendGoodInfo = data                }            }).catch(function(error) {                console.log(error);            });        } /**     * 返回监控,若缓存中返回标识为666,则弹出推荐权益卡弹窗,同时缓存中保存已经弹出推荐弹窗的标识     */    window.addEventListener("popstate", () => {        if(localStorage.getItem("stopBack")) {      //使用jquery或原生js的方式是弹窗显示            $(".showRecommendModal").fadeIn()        localStorage.removeItem('stopBack')            localStorage.setItem("hasBeenRecommended", true)        }    })参考:https://www.cnblogs.com/xyyt/p/10517187.html
