主要代码

router.ts


router.beforeEach((to, from, next) => {
  console.log(to, from)
  if (to.name !== "login") {
    if (sessionStorage.getItem("session")) {
      next()
    } else {
      next('/login?form=' + to.fullPath);

    }
  }
  next();
})

login.vue:

submitForm(value) {//登陆方法

    //...省略验证和存session 一般放在sessionStorage里面
    let fromUrl = unescape(location.search.split("=")[1]);
    this.$router.push(fromUrl);
  }