Set page title dynamically in Vue JS
Short article on different methods to set document title dynamically using document.title property or vue-meta packages in Vue JS application.
In this article, we will learn how to set/change page titles dynamically in Vue.
By default, in Vue, every page in your application will have the same title that you have set in your public index.html file or will just use the name by which you have named the project.
So, it doesn't matter which page you visit, the about page or the contact page, Vue will just use the same name as the page title.
So how do we set or change the title of the page based on the route we visit?
Well, there are two ways we can do it,
- Using the vue-meta module, and
- Using document.title property
Let's check it with an example.
Set page title using vue-meta in Vue
The vue-meta module lets us set not just page titles but also other meta information like descriptions, script, links, etc.
To use it in our Vue application, first, we have to install it and then configure it in the main.js file.
To install use:
npm i vue-meta // OR yarn add vue-meta
Next, we have to configure it in our main.js file like this
import Vue from 'vue' import VueMeta from 'vue-meta' Vue.use(VueMeta)
Once we have installed and configured it, we can use it to change page titles dynamically in your application like this.
<script> export default { metaInfo() { return { title: this.$route.name, }; }, } </script>
Using vue-meta you can also set dynamic descriptions and other information in the <head> tag of the document.
However, if you don't want to use any module to set the page title, then follow the next solution below.
Using document.title to set page title in Vue
The document.title property allows us to set or get the current page title from our document.
So here, we will use this property to set the current page title by assigning the name of the route to document.title property.
Since we are using vue-router in Vue, we can access the router instance using this.$route and to get the name of the route we use this.$route.name and then assign the name to ducument.title property in mounted() hook.
Example:
<script> export default { mounted(){ var title = this.$route.name console.log(title) document.title = title } } </script>
This will set the page title dynamically based on the route we visit in Vue.
Related Topics:
Related Posts
Easy Way to use localStorage with Vue
Short article on how to use local storage with Vue and learn saving and storing data in local storage with the setItem() and getItem() methods.
Force update Vue to Reload/Rerender component
Short tutorial on how to correctly force update Vue.js component to reload or rerender. The component is reinitialized without having to do a browser refresh.
Set and get cookies in a browser in Vue App
Find out how to set and get cookies in a browser for your webpage in Vue using vue-cookies package.
Save vuex state after page refresh in Vue App
Short tutorial on how to save (or persist) data in a vue application using Vuex and vuex-persist npm package.
