93 lines
2.7 KiB
JavaScript
93 lines
2.7 KiB
JavaScript
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
|
||
|
||
const routes= [
|
||
{
|
||
path: '/',
|
||
redirect: to => {
|
||
// 如果直接访问 /index.html,重定向到根路径的 start.html
|
||
if (window.location.pathname === '/index.html') {
|
||
window.location.href = '/';
|
||
return '/';
|
||
}
|
||
// 否则渲染 Homepage 组件
|
||
return { name: 'Homepage' };
|
||
}
|
||
},
|
||
{
|
||
path: '/Homepage',
|
||
name: 'Homepage',
|
||
component: ()=>import('./components/Homepage.vue'),
|
||
meta: { title: 'KDESIGN - 首页' }
|
||
},
|
||
{
|
||
path: '/ContactUs',
|
||
name: 'ContactUs',
|
||
component: ()=>import('./components/ContactUs.vue'),
|
||
meta: { title: 'KDESIGN - 联系我们' }
|
||
},
|
||
{
|
||
path: '/Aboutus',
|
||
name: 'Aboutus',
|
||
component: ()=>import('./components/Aboutus.vue'),
|
||
meta: { title: 'KDESIGN - 关于我们' }
|
||
},
|
||
{
|
||
path: '/ServiceProcess',
|
||
name: 'ServiceProcess',
|
||
component: ()=>import('./components/ServiceProcess.vue'),
|
||
meta: { title: 'KDESIGN - 服务流程' }
|
||
},
|
||
{
|
||
path: '/Examples',
|
||
name: 'Examples',
|
||
component: ()=>import('./components/Examples.vue'),
|
||
meta: { title: 'KDESIGN - 行业案例' }
|
||
},
|
||
{
|
||
path: '/ExampleItem',
|
||
name: 'ExampleItem',
|
||
component: ()=>import('./components/ExampleItem.vue'),
|
||
meta: { title: 'KDESIGN - 案例详情' }
|
||
},
|
||
{
|
||
path: '/Err404',
|
||
name: 'Err404',
|
||
component: ()=>import('./components/Tools/Err404.vue'),
|
||
meta: { title: '404:未找到此页面' }
|
||
},
|
||
{
|
||
path: '/:pathMatch(.*)*',
|
||
redirect: '/Err404'
|
||
}
|
||
]
|
||
|
||
const router = createRouter({
|
||
history: createWebHistory(),
|
||
routes,
|
||
scrollBehavior(to, from, savedPosition) {
|
||
// 如果有保存的滚动位置 (例如,当用户点击浏览器的后退/前进按钮时),则恢复该位置
|
||
if (savedPosition) {
|
||
return savedPosition;
|
||
} else {
|
||
// 如果目标路由和来源路由都是 Examples,则不改变滚动位置
|
||
if (to.name === 'Examples' && from.name === 'Examples') {
|
||
return false; // 保持当前滚动位置
|
||
}
|
||
// 否则,滚动到页面顶部
|
||
return { top: 0, left: 0, behavior: 'instant' };
|
||
}
|
||
}
|
||
})
|
||
|
||
// 路由守卫,切换页面时修改标题
|
||
router.beforeEach((to, from, next) => {
|
||
if (to.meta && to.meta.title) {
|
||
document.title = to.meta.title
|
||
} else {
|
||
document.title = '开动文化科技(北京)有限公司'
|
||
}
|
||
next()
|
||
})
|
||
|
||
export default router
|