router.ts 1.1 KB
Newer Older
dsyuan001's avatar
init  
dsyuan001 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import {createRouter, createWebHistory} from 'vue-router'
// @ts-ignore

const routes = [
  {
    path: '/',
    name: '/test',
    component: () => import('./views/test.vue')
  },
  {
    path: '/about',
    name: '/about',
    component: () => import('./views/about/index.vue'),
    meta: {
      title: '关于我们',
      keywords: '关键词3, 关键词4',
      description: '关于我们描述'
    }
  }
]
// console.log(routes)
// 配置路由
const router = createRouter({
dsyuan001's avatar
dsyuan001 已提交
24
  history: createWebHistory(import.meta.env.BASE_URL),
dsyuan001's avatar
init  
dsyuan001 已提交
25 26 27 28
  //history: createWebHashHistory(),
  routes: routes
})

dsyuan001's avatar
dev  
dsyuan001 已提交
29
router.afterEach((to) => {
dsyuan001's avatar
init  
dsyuan001 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  const {title, keywords, description} = to.meta;
  if (title) {
    document.title = title;
  }
  if (keywords) {
    const metaKeywords = document.querySelector('meta[name="keywords"]');
    if (metaKeywords) {
      metaKeywords.content = keywords;
    }
  }
  if (description) {
    const metaDescription = document.querySelector('meta[name="description"]');
    if (metaDescription) {
      metaDescription.content = description;
    }
  }
})
export default router