首頁  >  問答  >  主體

javascript - vue2.0如何設定 網頁標題 關鍵字 描述

vue2.0如何設定 網頁標題title 和meta標籤裡面的 關鍵字和描述呢?想動態改變,切換路由或在其他情況下,動態改變這三個地方

世界只因有你世界只因有你2642 天前923

全部回覆(2)我來回復

  • 黄舟

    黄舟2017-06-26 10:54:29

    在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')
    })

    回覆
    0
  • 高洛峰

    高洛峰2017-06-26 10:54:29

    入口檔案的基本標籤都是可以被操作的
    你可以在Router
    router.beforeEach((to, from, next) => {
    //這裡操作DOM
    // ...
    /* console.log (to);
    console.log(from);*/
    next();
    })

    回覆
    0
  • 取消回覆