想要实现vue动态改变页面title,需要给每个页面设置标题。并且在路由发生变化时修改页面title

router - index.js

const router = new Router({
    mode: 'history',
    routes: [
        {
            path: '/index',
            name: 'index',
            component: Index,
            meta:{
                // 页面标题title
                title: '首页'
            }
        },
        {
            path: '/content',
            name: 'content',
            component: Content,
            meta:{
                title: '内容'
            }
        }
    ]
})
export default router

main.js

import router from './router'
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
  • 2019-06-11
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
猜你喜欢
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案