Vue shared page, press the back button to go back to the home page

The content details page shared by Vue single page, the "Back" button is usually the route this.$router.push(-1), which is equivalent to the browser's return button. However, the shared page will become a blank page by pressing "Back". The business requirement is to jump to the home page or the desired page in the previous step.

The idea is that before entering the shared content page, the route judges whether there is a route on the from page, and it does not indicate that it is the first time to enter, then the "previous step" jumps to the specified route, otherwise the previous step is 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
            })
        }
    }

Guess you like

Origin blog.csdn.net/weixin_43968658/article/details/88060797