编程随笔 编程随笔
  • 前端
  • 后端
  • 嵌入式
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 嵌入式
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • 知识点

    • npm 和 yarn
    • 登录后重定向到原先路由
      • 编写对应vue文件
      • router.js定义路由
      • 路由守卫使用
    • patch-package使用
    • TortosseGit的ssh配置
    • nginx常用配置
    • mounted阶段获取不到dom的原因及解决方法
  • 代码调试

  • vue2

  • vue3

  • react

  • typescript

  • 前端
  • 知识点
华总
2024-01-19
0
0
目录

登录后重定向到原先路由原创

# 编写对应vue文件

# login.vue

<template>
... // 登录组件
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { message } from 'ant-design-vue';
import { postUserLogin } from '@/api/controller/UserController';
import router from '@/router';
import { LocationQuery, LocationQueryValue, useRoute } from "vue-router";
  
const route = useRoute()

/**
 * 按钮loading
 */
const loading = ref<boolean>(false);

const formState = reactive<UserLoginRequest>({
  userAccount: '',
  userPassword: '',
});


/**
 * 登录
 */
 function onSubmit() {
	loading.value = true;
	 postUserLogin(formState).then((res:any) => {
		 if (res.code === 0) {
			 localStorage.setItem('token', res.data.token);
			 const query: LocationQuery = route.query;
			const redirect = (query.redirect as LocationQueryValue) ?? "/";
			const otherQueryParams = Object.keys(query).reduce(
				(acc: any, cur: string) => {
					if (cur !== "redirect") {
						acc[cur] = query[cur];
					}
					return acc;
				},
				{}
			);

			 router.push({ path: redirect, query: otherQueryParams }).then(() => { 
					message.success('登录成功');
					loading.value = false;
			 })
			
		}
	}).catch((err) => {
		loading.value = false;
	});
}

</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

# redirect.vue

<template>
  <div ></div>
</template>

<script setup lang="ts">
import { useRoute, useRouter } from 'vue-router';

const route = useRoute();
const router = useRouter();

const { params, query } = route;
const { path } = params;

router.replace({ path: '/' + path, query });
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# router.js定义路由

{
    path: '/login',
    name: 'Login',
    component: () => import('@/views/gen/login/index.vue')
  },
  {
    path: "/redirect/:path(.*)",
    component: () => import("@/views/gen/redirect/index.vue"),
  },
1
2
3
4
5
6
7
8
9

# 路由守卫使用

import router from "@/router";

// 白名单路由
const whiteList = ["/login"];

router.beforeEach(async (to, from, next) => {
    const hasToken = localStorage.getItem("token");
    if (hasToken) {
        if (to.path === "/login") {
            next({ path: "/" });
        } else {
            // 未匹配到任何路由,跳转404
            if (to.matched.length === 0) {
                from.name ? next({ name: from.name }) : next("/404");
            } else {
                next();
            }
        }
       
    } else {
        // 未登录可以访问白名单页面
        if (whiteList.indexOf(to.path) !== -1) {
            next();
        } else {
            next(`/login?redirect=${to.path}`);
           
        }
    }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
上次更新: 2025/02/18 14:46:10
npm 和 yarn
patch-package使用

← npm 和 yarn patch-package使用→

最近更新
01
jFlash使用 原创
03-24
02
中央仓库上传指南 原创
03-23
03
模板生成工具 原创
02-18
04
RTC实时时钟 原创
02-12
05
keil调试 原创
01-21
更多文章>
Copyright © 2023-2025 liyao52033  All Rights Reserved
备案号:鄂ICP备2023023964号-1    
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式