在 router.js 中需要在每一个路由下面多添加一个 meta:{title:‘主页’},比如:
export default new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld,
meta: {
title: '首页'
}
},
{
path: '/index',
name: 'index',
component: index,
meta: {
title: '登录'
}
},
{
path: '/new_file',
name: 'new_file',
component: new_file,
meta: {
title: '个人信息'
}
}
]
})
然后在 main.js里面添加以下代码即可:
/* 路由发生变化修改页面title */
router.beforeEach((to, from, next) => {
if (to.meta.title) {
document.title = to.meta.title
}
next()
})
感谢您的阅读, 如有什么不妥之处,还请赐教,我会继续努力,分享更好的前端代码。
点赞和在看就是最大的支持❤️