Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  81] [ 5]  / answers: 1 / hits: 19495  / 6 Years ago, fri, april 6, 2018, 12:00:00

I used vue-cli with webpack to build up the vue project.
Then I installed vue-meta-info to set up the seo.



I want to set up the Page title with the templates and the route name.
However , I cannot get the variable in router.



rotuer/index.js



import Vue from 'vue';
import Router from 'vue-router';
import HelloWorld from '@/components/HelloWorld';

Vue.use(Router);

export default new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld,
},
],
});


App.vue



<template>
<div id=app>
<router-view/>
</div>
</template>

<script>
export default {
name: 'App',
metaInfo: {
title: My Website - + route.name,
},
};

</script>

More From » vue.js

 Answers
13

You can use the router's beforeEach() helper.



Example:



routes.js



import Foo from '@/components/Foo'

export default new VueRouter({
mode: 'history',
routes: [
{
path: '/',
name: 'Foo',
component: Foo,
meta: {
title: 'Foo'
}
}
]
})


In app.js or main.js or whatever your main Javascript file is. Placing the code in one of the aforementioned JS file will allow all pages to update the title accordingly and is a much cleaner way to handle the titles.



import router from '@/routes'

// your other codes here

router.beforeEach((to, from, next) => {
document.title = `My website - ${to.meta.title}`
next()
})

[#54746] Wednesday, April 4, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
agustindejonm

Total Points: 738
Total Questions: 84
Total Answers: 84

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
agustindejonm questions
Fri, Jun 25, 21, 00:00, 3 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Sat, May 16, 20, 00:00, 4 Years ago
;