我使用vue-cli和webpack来构建vue项目。 然后我安装了vue-meta-info来设置seo。
我想使用模板和路径名设置页面标题。 但是,我无法在路由器中获取变量。
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>
答案 0 :(得分:7)
您可以使用路由器的beforeEach()
帮助程序。
示例:
import Foo from '@/components/Foo'
export default new VueRouter({
mode: 'history',
routes: [
{
path: '/',
name: 'Foo',
component: Foo,
meta: {
title: 'Foo'
}
}
]
})
在app.js
或main.js
或您的主要Javascript文件中。将代码放在上述JS文件之一中将允许所有页面相应地更新标题,并且是处理标题的更清晰的方式。
import router from '@/routes'
// your other codes here
router.beforeEach((to, from, next) => {
document.title = `My website - ${to.meta.title}`
next()
})
答案 1 :(得分:0)
您需要从每个组件到根组件进行更改标题,一种方法是$on .... $emit
,
const Door = { template: '<div>Door</div>',
mounted: function () {
vm.$emit('title', this.$route.name);
}
}
const Bedroom = { template: '<div>Bedroom</div>',
mounted: function () {
vm.$emit('title', this.$route.name);
}
}
const Kitchen = { template: '<div>Kitchen</div>',
mounted: function () {
vm.$emit('title', this.$route.name);
}
}
const Hall = { template: '<div>Hall</div>',
mounted: function () {
vm.$emit('title', this.$route.name);
}
}
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '/', component: Door, name:"You are in door"},
{ path: '/bedroom', component: Bedroom, name:"You are in Bedroom"},
{ path: '/kitchen', component: Kitchen, name:"You are in kitchen"},
{ path: '/hall', component: Hall, name:"You are in hall"},
]
})
var vm = new Vue({
router,
el: '#app',
data: {
msg: 'Hello World',
title:'no title set'
},
mounted: function(){
this.$on('title', function (title) {
this.title = title;
})
}
})
&#13;
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<div>Title : {{title}}</div>
<router-link to="/">Door</router-link>
<router-link to="/bedroom"> | Bedroom</router-link>
<router-link to="/kitchen"> | Kitchen</router-link>
<router-link to="/hall"> | Hall</router-link>
<router-view></router-view>
</div>
&#13;
另一种方法是使用$parent
,
const Door = { template: '<div>Door</div>',
mounted: function () {
this.$parent.title = this.$route.name;
}
}
const Bedroom = { template: '<div>Bedroom</div>',
mounted: function () {
this.$parent.title = this.$route.name;
}
}
const Kitchen = { template: '<div>Kitchen</div>',
mounted: function () {
this.$parent.title = this.$route.name;
}
}
const Hall = { template: '<div>Hall</div>',
mounted: function () {
this.$parent.title = this.$route.name;
}
}
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '/', component: Door, name:"You are in door"},
{ path: '/bedroom', component: Bedroom, name:"You are in Bedroom"},
{ path: '/kitchen', component: Kitchen, name:"You are in kitchen"},
{ path: '/hall', component: Hall, name:"You are in hall"},
]
})
new Vue({
router,
el: '#app',
data: {
msg: 'Hello World',
title:'no title set'
}
})
&#13;
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<div>Title : {{title}}</div>
<router-link to="/">Door</router-link>
<router-link to="/bedroom"> | Bedroom</router-link>
<router-link to="/kitchen"> | Kitchen</router-link>
<router-link to="/hall"> | Hall</router-link>
<router-view></router-view>
</div>
&#13;