javascript - vue2.0如何設定 網頁標題 關鍵字 描述
世界只因有你
世界只因有你 2017-06-26 10:52:29
0
2
922

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

世界只因有你
世界只因有你

全部回覆(2)
黄舟

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

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!