this.$route.fullPath获取当前页面的路由地址

137 阅读1分钟

当我们从一个页面退到登录页面后,重新登录,如何回到我们刚才浏览的页面

这种场景在很多页面中使用,当我们没有登陆浏览一个页面的信息,该页面会提示我们进行登录,才能继续浏览该信息。当我们登录成功之后,它会自动跳转到我们刚才浏览的页面,这个功能实现的方式如下。

我们跳转到登录页面的时候,我们会使用this.$route.fullPath获取当前浏览的路由地址,让登录页面redirect携带当前页面的路由,让下一次登录直接redirect当前页面

跳转到登录页:

   console.log(this.$route.fullPath) //获取当前路由的地址
   // this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      this.$router.push({
        path: '/login',
        query: {
          redirect: this.$route.fullPath  //携带当前路由地址
        }
      })

进行的登录:

 // 登录成功后跳转到
      this.$router.push(this.$route.query.redirect || '/')