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

基本环境配置: webpack + vue2.0 + vue-router +nodeJS

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

以上这篇使用vue-router设置每个页面的title方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容