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
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')
})
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();
})