Vue分享出去的页面,按回退按钮回退到首页

Vue单页面分享出去的内容详情页,其中的 “上一步” 按钮通常是用路由this.$router.push(-1),等价于浏览器的返回按钮。但是分享出去的页面按“上一步”就会变成空白页,业务需求是可以上一步跳转到首页或者想要的页面。

思路就是进入分享内容页前,路由判断from的页面有没有路由,没有说明是首次进入,那么"上一步"就跳转到指定路由,否则上一步就是go(-1);

    data:(){    
        return {
            firstEnter: false        
        }
    },
    methods:{
        goBack(){
            if (this.firstEnter){
                this.$router.push({path:"/index"})
            } else {
                this.$router.go(-1)
            }       
        }
    },
    beforeRouterEnter(to,from,next) {
        if (from.name == null) {
            next( vm => {
                vm.firstEnter = true
            })
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_43968658/article/details/88060797
今日推荐