In an effort to modify page titles, I have developed a mixin using document.title
and global mixins.
The contents of my mixin file (title.ts) are as follows:
import { Vue, Component } from 'vue-property-decorator'
function getTitle(vm: any): string {
const title: string = vm.title
if (title) {
return `${title} | site.com`
}
return 'Admin panel | site.com'
}
@Component
export default class TitleMixin extends Vue {
public created(): void {
const title: string = getTitle(this)
if (title) {
document.title = title
}
}
}
Next, I registered this mixin globally in main.ts:
import titleMixin from '@/mixins/title'
Vue.mixin(titleMixin)
To set up the title in a specific Vue component, follow these steps:
@Component
export default class Login extends Vue {
public title: string = 'New title'
}
My project consists of approximately 5 components. By utilizing console.log
within the mixin, I can observe that it is invoked in each component sequentially, eventually setting the document.title
based on the last component's created()
hook.
Is there a more proper way to establish the title for the CURRENT page?