使用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名字,最后在遍歷:

下面代碼加在main.js里面

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM