Vue-Meta: Adding MetaData to Vue.js Components
4 min read
What is vue-meta?
“vue-meta” is a module that offers a Vue plugin which allows us to add metadata from component dynamically.
This means in projects where we have multiple routes and we want to update the meta tags for SEO dynamically based on the route currently rendered on the page, vue-meta will handle that for us while giving us control over the app metadata.
Setup
First of all, we need to add vue-meta to our project and let Vue know that we want to use it as a plugin available to all components.
1npm install vue-meta --save
Then, we add the vue-meta to our main js file.
1// main.js or index.js2import Vue from "vue";3import App from "./App.vue"; // main component4import Meta from "vue-meta";5Vue.use(Meta);6new Vue({7render: (h) => h(App),8}).$mount("#app");
Adding Metadata
Now we look into an example of how to add metadata to our component.
1<template>2<div id="app">3<img width="25%" src="./assets/logo.png" />4<HelloWorld msg="Hello Vue in CodeSandbox!" />5</div>6</template>7<script>8import HelloWorld from "./components/HelloWorld";9export default {10name: "App",11components: {12HelloWorld,13},14metaInfo() {15return {16title: "test meta data with vue",17meta: [18{19vmid: "description",20name: "description",21content:22"hello world, this is an example of adding a description with vueMeta",23},24],25};26},27};28</script>
As can be seen, we can do that by calling metaInfo
function and return an object as a value that will contain our metadata.
In addition, we can set the meta value dynamically based on some logic as we have access to it at a component level.
1<template>2<div id="app">3<img width="25%" src="./assets/logo.png" />4<HelloWorld msg="Hello Vue in CodeSandbox!" />5</div>6</template>7<script>8import HelloWorld from "./components/HelloWorld";9export default {10name: "App",11components: {12HelloWorld,13},14metaInfo() {15const a = "test";16return {17title: "test meta data with vue",18meta: [19...(a === "test" && [20{21vmid: "description",22name: "description",23content:24"hello world, this is an example of adding a description with vue-meta",25},26]),27],28};29},30};31</script>
Type of Metadata
We can add more or less any type of metadata needed using ‘vue-meta’ plugin whether it is meta, title, link or script.
In the following below we will see an example of how to add some of these metadata.
1<script>2import HelloWorld from "./components/HelloWorld";3export default {4name: "App",5components: {6HelloWorld,7},8metaInfo() {9const a = "test";10return {11title: "test meta data with vue",12meta: [13...(a === "test" && [14{15vmid: "description",16name: "description",17content:18"hello world, this is an example of adding a description with vue-meta",19},20]),21],22script: [23{24src: "<https://services.postcodeanywhere.co.uk/js/address-3.91.js>",25async: true,26defer: true,27body: true,28},29],30link: [31{32rel: "canonical",33href: "<https://malikgabroun.com/>",34},35],36};37},38};39</script>
In the above example, we can see how we added an external script to the body using vue-meta. In the case we wanted the script to be included in the head instead, we can do that by removing body flag.
Vmid
So far, we looked into how to setup vue-meta and add metadata to our component dynamically, however, what if we wanted to set the value to the specific property in multiple components and how that would be resolved.
In order to do that, we can use vmid which is a special property that vue-meta provide us in order to resolve the value within the component tree. So if two sets of meta have the same vmid it will override it, using the value from the last updated component (i.e. child component) instead of merging it.
Conclusion
In conclusion, vue-meta is a plugin that in most vue frameworks will come out of the box which allows us and give us the control to how the metadata should show in a website.