Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Created March 22, 2025 09:58
Show Gist options
  • Save atomjoy/ad6192d983bfd55035d7eb6450330858 to your computer and use it in GitHub Desktop.
Save atomjoy/ad6192d983bfd55035d7eb6450330858 to your computer and use it in GitHub Desktop.
Change page title in Vue 3.
<template></template>
<script setup>
import { useI18n } from 'vue-i18n';
import { watch, onMounted } from 'vue';
const { t, locale } = useI18n({ useScope: 'global' });
const props = defineProps({
title: {
type: String,
default: '',
},
});
onMounted(() => {
document.title = t(props.title);
});
watch(
() => locale.value,
(lang) => {
document.title = t(props.title);
}
);
watch(
() => props.title,
(lang) => {
document.title = t(props.title);
}
);
</script>
<!--
// Require in main.js createI18n(options)
allowComposition: true,
globalInjection: true,
legacy: false,
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment