Home  >  Q&A  >  body text

javascript - How to set web page title keyword description in vue2.0

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

世界只因有你世界只因有你2694 days ago1015

reply all(2)I'll reply

  • 黄舟

    黄舟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')
    })

    reply
    0
  • 高洛峰

    高洛峰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();
    })

    reply
    0
  • Cancelreply