進入 router 文件夾底下的index.js文件vue
首先引入:vue-router
import Vue from 'vue'
import Router from 'vue-router'
而後在路由裏面配置每一個路由的地址:spa
routes: [ { /* (首頁)默認路由地址 */ path: '/', name: 'Entrance', component: Entrance, meta: { title: '首頁入口' } }, { /* 修改暱稱 */ path: '/modifyName/:nickName', name: 'modifyName', component: modifyName, meta: { title: '修改暱稱' } }, { /* 商品詳情 */ path: '/goodsDetail', name: 'goodsDetail', component: goodsDetail, meta: { title: '商品詳情' } }, { /* Not Found 路由,必須是最後一個路由 */ path: '*', component: NotFound, meta: { title: '找不到頁面' } } ]
在每個meta裏面設置頁面的title名字,最後在遍歷code
router.beforeEach((to, from, next) => { /* 路由發生變化修改頁面title */ if (to.meta.title) { document.title = to.meta.title } next() })
這樣就爲每個VUE 的頁面添加了titlecomponent