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

进入 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

相关文章
相关标签/搜索