How to set the keywords and descriptions in the webpage title and meta tag in vue2.0? If you want to dynamically change, switch routes or other situations, dynamically change these three places
黄舟2017-06-26 10:54:29
Set as follows in router.js
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
const router = new Router({
routes: [{
path: '/login',
component: Login,
meta: { title: '登录' }
}, {
path: '/register',
component: Register,
meta: { title: '注册' }
}
]
})
// 全局配置
router.beforeEach((to, from, next) => {
// Change doc title
document.title = to.meta.title || 'Unknow title'
document.querySelector('meta[name="keywords"]').setAttribute('content', 'keywords')
document.querySelector('meta[name="description"]').setAttribute('content', 'description')
})
高洛峰2017-06-26 10:54:29
The basic tags of the entry file can be manipulated
You can operate the DOM in Router
router.beforeEach((to, from, next) => {
//Manipulate DOM here
// ...
/* console. log(to);
console.log(from);*/
next();
})