./src/mixins/title.js
function getTitle(vm) {
const { title } = vm.$options;
if (title) {
return typeof title === 'function'
? title.call(vm)
: title;
}
}
export default {
mounted() {
const title = getTitle(this);
if (title) {
document.title = title;
}
}
};
./src/main.js
import mixinTitle from './mixins/title';
Vue.mixin(mixinTitle);
./src/views/example.vue
<script>
export default {
title: 'Example page title'
};
</script>
or:
./src/views/example2.vue
<script>
export default {
title() {
const someValue = 'Another Example';
return `Example page title: ${someValue}`;
}
};
</script>
Based on Evan You's title mixin.