使用vue-router设置每个页面的title

进入 router 文件夹底下的index.js文件

首先引入:

import Vue from 'vue'
import Router from 'vue-router'

然后在路由里面配置每个路由的地址:

 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名字,最后在遍历

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

这样就为每一个VUE 的页面添加了title

原文地址:https://www.cnblogs.com/LChenglong/p/8984555.html