Created
March 22, 2025 10:04
-
-
Save atomjoy/9c0c905458ac6e8a1dd06df42d38b4fa to your computer and use it in GitHub Desktop.
Add page meta tags in Vue 3.
This file contains 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 } from 'vue'; | |
import { useI18n } from 'vue-i18n'; | |
const { t, locale } = useI18n({ useScope: 'global' }); | |
const props = defineProps({ | |
json: { | |
type: [Array, Object], | |
default: [], | |
}, | |
}); | |
const head = document.querySelector('head'); | |
watch( | |
() => props.json, | |
function (n, o) { | |
try { | |
let arr = JSON.parse(props.json); | |
arr.forEach((i) => { | |
const meta = document.createElement('meta'); | |
meta.setAttribute(i.attribute, i.value); | |
meta.setAttribute('content', t(i.content)); | |
head.appendChild(meta); | |
}); | |
} catch (err) {} | |
} | |
); | |
</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