首页 > 解决方案 > 在 Vue 中,如何使用 window.history.go(-1) 使浏览器返回上一页

问题描述

嗨,我正在制作一个基于Vue的网页,但我现在遇到了麻烦。我最初使用router.go(-1)来阻止通过地址访问(例如: ../client/mypage -> ../admin/mypage )并重定向到上一页,但它不起作用,因为页面本身已刷新。(在我看来,路由器历史堆栈是空的)

所以,我发现的是window.history.go(-1),但即使它的长度大于 1,它也不起作用。

有什么问题,我应该如何解决?

    mounted(){
        if (this.auth) { // when you access through address, check the authority  
            this.$router.go(-1); //doesn't work
            window.history.go(-1); //doesn't work either
        }
    }

图片:console.log(window.history)

标签: javascriptvue.jsbrowserrouter

解决方案


试试这个。

mounted(){
        if (!this.auth) {  //If not authenticated
            window.history.back();
        }
}

推荐阅读