How to set a document title in Vue app

How to set a document title in Vue app

In this post we will give you information about How to set a document title in Vue app. Hear we will give you detail about How to set a document title in Vue appAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to set a document <title> to the pages in a Vue app using the vue-meta (npm) package.

Installing the vue-meta package

The vue-meta package helps us to set the document title and other meta tags to the current page.

Run the following command to install the package.

npm install vue-meta

To configure the package add the following (highlighted) lines to your main.js file.

main.js
import Vue from "vue";import App from "./App.vue";import VueMeta from 'vue-meta';Vue.use(VueMeta);new Vue({  render: h =&gt; h(App)}).$mount("#app");

Setting the title

Now, inside our vue components, we can set a title to the page using the metaInfo property.

Here is an example that sets the title to the About page.

About.vue
&lt;template&gt;  &lt;div id="app"&gt;    &lt;h1&gt;This About Us Page&lt;/h1&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;  export default {    name: 'About',    metaInfo: {      title: 'About Us'    }  }&lt;/script&gt;

It will render an HTML <title> tag in your page like this.

&lt;title&gt;About Us&lt;/title&gt;

If you don’t like to use an external package to set the title, then you can use the document.title property inside the created() lifecycle hook.

About.vue
&lt;template&gt;  &lt;div id="app"&gt;    &lt;h1&gt;This About Us Page&lt;/h1&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;  export default {    name: 'About',    created(){        document.title = "About Us"    }  }&lt;/script&gt;

Hope this code and post will helped you for implement How to set a document title in Vue app. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US