ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
 
import { resetStaticRoutes } from '@vben/utils';
 
import { APP_PREFIX } from '#/utils/constants';
import { getJnpfAppEnCode } from '#/utils/jnpf';
 
import { createRouterGuard } from './guard';
import { routes } from './routes';
 
// 取路径上的应用识别码的作为基础路由
let VITE_BASE = import.meta.env.VITE_BASE || '/';
const appEnCode = getJnpfAppEnCode();
if (appEnCode) {
  VITE_BASE = `/${APP_PREFIX + appEnCode}`;
} else {
  if (routes[0]?.children && routes[0].children[0]?.meta) {
    routes[0].children[0].meta.i18nCode = 'routes.basic.dashboard';
    routes[0].children[0].meta.title = '控制台';
  }
}
 
/**
 *  @zh_CN 创建vue-router实例
 */
const router = createRouter({
  history: import.meta.env.VITE_ROUTER_HISTORY === 'hash' ? createWebHashHistory(VITE_BASE) : createWebHistory(VITE_BASE),
  // 应该添加到路由的初始路由列表。
  routes,
  scrollBehavior: (to, _from, savedPosition) => {
    if (savedPosition) {
      return savedPosition;
    }
    return to.hash ? { behavior: 'smooth', el: to.hash } : { left: 0, top: 0 };
  },
  // 是否应该禁止尾部斜杠。
  // strict: true,
});
 
const resetRoutes = () => resetStaticRoutes(router, routes);
 
// 创建路由守卫
createRouterGuard(router);
 
export { resetRoutes, router };