Created
March 22, 2025 09:57
-
-
Save atomjoy/8ed2b63754bbcfae1f2aecec02449610 to your computer and use it in GitHub Desktop.
Change page description in Vue 3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template></template> | |
<script setup> | |
import { watch, onMounted } from 'vue'; | |
import { useI18n } from 'vue-i18n'; | |
const { t, locale } = useI18n({ useScope: 'global' }); | |
const props = defineProps({ | |
description: { | |
type: String, | |
default: '', | |
}, | |
}); | |
const el = document.querySelector('head meta[name="description"]'); | |
onMounted(() => { | |
el.setAttribute('content', t(props.description)); | |
}); | |
watch( | |
() => locale.value, | |
(lang) => { | |
el.setAttribute('content', t(props.description)); | |
} | |
); | |
watch( | |
() => props.description, | |
(lang) => { | |
el.setAttribute('content', t(props.description)); | |
} | |
); | |
</script> | |
<!-- | |
// Required 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