首先在router/index.js里給每個(gè)對(duì)象定義meta
{
path:'',
redirect:'/home'
},
{
path:'/home',
component:Home,
meta:{
title:'首頁(yè)'
}
},
{
path:'/cate',
component:Cate,
meta:{
title:'菜單'
}
},
{
path:'/Shopcart',
component:Shopcart,
meta:{
title:'購(gòu)物車'
}
},
{
path:'/Profile',
component:Profile,
meta:{
title:'我的'
}
}
第二步在mainjs里寫一個(gè)箭頭函數(shù)
router.beforeEach((to, from, next) => {
/* 路由發(fā)生變化修改頁(yè)面title */
if (to.meta.title) {
document.title = to.meta.title
}
next()
})