index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // createRouter:创建router实例对象
  2. // createWebHistory:创建history模式的路由
  3. import { createRouter, createWebHistory } from 'vue-router'
  4. import IndexView from '@/views/IndexView.vue'
  5. import AboutView from '@/views/Aboutview.vue'
  6. import ProductView from '@/views/ProductView.vue'
  7. import ProductDetailsView from '@/views/ProductDetailsView.vue'
  8. import ExampleView from '@/views/ExampleView.vue'
  9. import NewsView from '@/views/NewView.vue'
  10. import NewsDetailsView from '@/views/NewsDetailsView.vue'
  11. import Solution from '@/views/Solution.vue'
  12. import MainBusiness from '@/views/MainBusiness.vue'
  13. import ContactInformation from '@/views/ContactInformation.vue'
  14. import JoinUs from '@/views/JoinUs.vue'
  15. import News from '@/views/News.vue'
  16. import newsDetail from '@/views/newsDetail.vue'
  17. import subGroup from '@/views/subGroup.vue'
  18. const router = createRouter({
  19. history: createWebHistory(),
  20. routes: [
  21. { path: '/', redirect: '/index' },
  22. { path: '/index', component: IndexView },
  23. { path: '/product', component: ProductView },
  24. { path: '/product/productId', component: ProductDetailsView },
  25. { path: '/example', component: ExampleView },
  26. { path: '/news', component: News },
  27. { path: '/news/newsId', component: NewsDetailsView },
  28. { path: '/about', component: AboutView },
  29. { path: '/overview', component: Solution },
  30. { path: '/MainBusiness', component: MainBusiness },
  31. { path: '/ContactInformation', component: ContactInformation },
  32. {path:'/JoinUs', component: JoinUs},
  33. {path:'/news',component: News},
  34. {path:'/newsDetail/:id?',component: newsDetail},
  35. {path:'/subGroup/:id?',component: subGroup}
  36. ]
  37. })
  38. // 路由守卫:确保每次路由跳转时语言状态正确
  39. router.beforeEach((to, from, next) => {
  40. // 导入语言管理工具
  41. import('@/utils/language.js').then(({ getCurrentLanguage }) => {
  42. const currentLang = getCurrentLanguage();
  43. // 确保 i18n 的语言设置与 localStorage 一致
  44. import('../../i18n/index.js').then((i18nModule) => {
  45. const i18n = i18nModule.default;
  46. if (i18n.global.locale.value !== currentLang) {
  47. i18n.global.locale.value = currentLang;
  48. console.log(`路由跳转时同步语言: ${currentLang}`);
  49. }
  50. });
  51. });
  52. next();
  53. });
  54. export default router