router动态设置页面title

  作者:chrispy

import Vue from 'vue'import Router from 'vue-router'Vue.use(Router)const router = new Router({ // mode: 'history', routes: [ { path: '/', redirect: '/informationConfig' }, { path: '*'

import Vue from 'vue'

import Router from 'vue-router'


Vue.use(Router)


const router = new Router({

  // mode: 'history',

  routes: [

    {

      path: '/',

      redirect: '/informationConfig'

    },

    {

      path: '*',

      redirect: '/404'

    },

    {

      path: '/404',

      name: 'NotPage',

      component: function (resolve) {

        require(['../views/404.vue'], resolve)

      },

      meta: {

        pageTitle: '404'

      }

    },

    {

      path: '/500',

      name: 'Error',

      component: function (resolve) {

        require(['../views/500.vue'], resolve)

      },

      meta: {

        pageTitle: '500'

      }

    },

    {

      path: '/index',

      name: 'Index',

      redirect: '/informationConfig',

      component: function (resolve) {

        require(['../views/index.vue'], resolve)

      },

      meta: {

        pageTitle: '首页'

      },

      children: [

        {

          path: '/informationConfig',

          name: 'InformationConfig',

          component: function (resolve) {

            require(['../views/informationConfig.vue'], resolve)

          },

          meta: {

            pageTitle: '资料登记配置',

            page: '资料登记配置'

          }

        },

        {

          path: '/newInformationConfig',

          name: 'NewInformationConfig',

          component: function (resolve) {

            require(['../views/newInformationConfig.vue'], resolve)

          },

          meta: {

            pageTitle: '资料登记配置',

            page: '新增资料登记'

          }

        },

        {

          path: '/registerInfoCustomer',

          name: 'RegisterInfoCustomer',

          component: function (resolve) {

            require(['../views/registerInfoCustomer.vue'], resolve)

          },

          meta: {

            pageTitle: '已登记客户资料管理',

            page: '已登记客户资料'

          }

        }

      ]

    }

  ]

})


router.beforeEach(function (to, from, next) {

  let setDocumentTitle = function (title) {

    document.title = title

  }

  to.meta.pageTitle && setDocumentTitle(to.meta.pageTitle)

  next()

})


export default router


有用  |  无用

猜你喜欢