import { createWebHistory, createRouter } from 'vue-router';


// 公共路由
export const constantRoutes = [
    {
        path: '/',
        redirect: '/index',
    },
    {
        path: '/index',
        component: () => import('@/views/index/index.vue'),
    },
    {
        path: '/detail/:id',
        component: () => import('@/views/detail/index.vue'),
    },
    {
        path: '/:pathMatch(.*)*',
        component: () => import('@/views/error/404.vue'),
    },
    {
        path: '/401',
        component: () => import('@/views/error/401.vue'),
    },
    {
        path: '',
        redirect: '/index',
    },
];


/**
 * 创建路由
 */
const router = createRouter({
    history: createWebHistory('/'),
    routes: constantRoutes,
    // 刷新时，滚动条位置还原
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            return { top: 0 };
        }
    }
});

export default router;
