贴代码:想每次进一个路由的时候,改一下document.titleimport Vue from 'vue'import Router from 'vue-router'Vue.use(Router)
const router = new Router({
routes: [ //首页
{
path: '/',
name: 'index',
component: () => import('../components/index.vue'),
meta: {
title: '首页'
}
}, //详情页
{
path: '/detail',
name: 'Detail',
component: () => import('../components/Detail.vue'),
meta: {
title: '详情页'
}
}
]
});
router.beforeEach((to, from, next) => { if(to.meta.title) { document.title = to.meta.title;
}
});export default router去掉router.beforeEach之后,router-view就出现了,然后document.title没改,加回router.beforeEach之后,router-view就消失了,然后document.title改了。
2 回答
波斯汪
TA贡献1811条经验 获得超4个赞
router.beforeEach((to, from, next) => { if(to.meta.title) { document.title = to.meta.title; } next(); // 记得调用 next});
添加回答
举报
0/150
提交
取消