Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change page titles when using vue-router?

I would like to specify my titles within the route definition if possible. What is normally specified in <head><title> and appears in the browser title bar.

I have my project set up as follows:

main.js

import Vue from 'vue' import App from './App.vue' import VeeValidate from 'vee-validate'; import router from './router' import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css';  Vue.use(VeeValidate); Vue.use(ElementUI); Vue.config.productionTip = false  new Vue({     router,     render: h => h(App) }).$mount('#app') 

router.js

import Vue from 'vue' import Router from 'vue-router' import Skills from './components/Skills.vue' import About from './components/About.vue'  Vue.use(Router)  export default new Router({   routes: [     {       path: '/',       name: 'skills',       component: Skills,       meta: { title: 'Skills - MyApp' } // <- I would to use this one     },     {       path: '/about/:name',  // Add /:name here       name: 'about',       component: About,       meta: { title: 'About - MyApp' }     }   ] }) 

Preferably, I would want an automatic system instead of changing page title on the created function of every component. Thanks.

like image 536
darkhorse Avatar asked Aug 01 '18 18:08

darkhorse


People also ask

How do I change the title on my vue router?

This can be simply done by changing the content of the <title> tag in our html code. But for Single-Page Applications, things are a little bit different. When the router changes to a different page component, the title should be changed programmatically using document. title = 'new title' .

How do I change Meta on vue?

Using vue-meta First, to use vue-meta , open your terminal and navigate to your existing Vue project directory. Then, run the following command: npm install vue-meta @2.4. 0.

How do I redirect to another page in vue?

You can use $router. push({ name: "yourroutename"}) or just router. push("yourroutename") now to redirect.


1 Answers

You can use a navigation guard with the router definition:

import Vue from 'vue';  const DEFAULT_TITLE = 'Some Default Title'; router.afterEach((to, from) => {     // Use next tick to handle router history correctly     // see: https://github.com/vuejs/vue-router/issues/914#issuecomment-384477609     Vue.nextTick(() => {         document.title = to.meta.title || DEFAULT_TITLE;     }); }); 

You'll need to change your export to:

const router = new Router({ ... }); ... export default router; 

Or you can use an immediate watcher on your root component:

export default {     name: 'App',     watch: {         $route: {             immediate: true,             handler(to, from) {                 document.title = to.meta.title || 'Some Default Title';             }         },     } }; 
like image 76
Steven B. Avatar answered Sep 25 '22 23:09

Steven B.