我是靠谱客的博主 细腻烧鹅,这篇文章主要介绍vue 跳转路由修改页面title为对应路由名字,现在分享给大家,希望可以做个参考。

vue-router 跳转页面的时候,修改页面的title为对应的路由

 routes: [
    {
      // 默认的首页
      path: '/',
      name: 'Home',
      component: Home,
      meta: {
        index: 0,
        title: '首页'
      }
    },
    {
      // 选择城市
      path: '/city',
      name: 'City',
      component: City,
      meta: {
        index: 1,
        title: '选择城市'
      }
    }
]

在main.js中获取路由的title,去替换为页面的title

添加一个导航守卫, 拦截和替换路由中配置了title名的路由

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

 

最后

以上就是细腻烧鹅最近收集整理的关于vue 跳转路由修改页面title为对应路由名字的全部内容,更多相关vue内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部