实现:切换页面切换标题,扩展 vue-router 的类型

布局容器-页面标题

网址:https://router.vuejs.org/zh/guide/advanced/meta

给每一个路由添加 元信息 数据
router/index.ts

const router = createRouter({
   
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
    path: '/login', component: () => import('@/views/Login/index.vue'), meta: {
    title: '登录' } },
    {
   
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
   
          path: '/home',
          component: () => import('@/views/Home/index.vue'),
          meta: {
    title: '首页' }
        },
        {
   
          path: '/notify',
          component: () => import('@/views/Notify/index.vue'),
          meta: {
    title: '消息通知' }
        },
        {
   
          path: '/user',
          component: () => import('@/views/User/index.vue'),
          meta: {
    title: '个人中心' }
        }
      ]
    }
  ]
})
//后置导航
//切换路由设置标题
router.afterEach((to) => {
   
  document.title = `${
     to.meta.title || ''}-优医问诊`
})

扩展元信息类型 types/vue-router.d.ts

import 'vue-router'
declare module 'vue-router' {
   
  // 扩展 元信息类型
  interface RouteMeta {
   
    // 标题
    title?: string
  }
}

在这里插入图片描述

相关推荐

  1. [Vue3]-router实现基本页面跳转

    2023-12-09 10:18:02       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 10:18:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 10:18:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 10:18:02       82 阅读
  4. Python语言-面向对象

    2023-12-09 10:18:02       91 阅读

热门阅读

  1. mysql怎么优化查询?

    2023-12-09 10:18:02       45 阅读
  2. HTB Devvortex

    2023-12-09 10:18:02       48 阅读
  3. 理解chatGPT的Function calling

    2023-12-09 10:18:02       51 阅读
  4. kafka主题分区副本集群的概念

    2023-12-09 10:18:02       58 阅读
  5. 通讯app:

    2023-12-09 10:18:02       57 阅读
  6. 图论——最小生成树

    2023-12-09 10:18:02       43 阅读
  7. 力扣230. 二叉搜索树中第K小的元素

    2023-12-09 10:18:02       58 阅读