如果您将 title 属性定义为函数,则可以:
{
meta: { title: route => { /* return custom title based on route, store or anything */ } }
}
和
router.beforeEach((to, from, next) => {
if (to.meta.title) {
document.title = to.meta.title(to);
}
next();
})
Codepen:https ://codepen.io/anon/pen/roRmdo?editors=1111 (您需要检查内部 iframe 以查看标题更改)。
或创建一个指令:
Vue.directive('title', {
inserted: (el, binding) => document.title = binding.value,
update: (el, binding) => document.title = binding.value
})
然后在路由器视图组件上使用该指令:
<router-view v-title="title" ></router-view>
零件:
export default {
data(){
return {
title: 'This will be the title'
}
}
}
来源:https ://github.com/vuejs/vue-router/issues/914