一般来说vue单页面应用的title在index.html中设置,但是一旦设置就是唯一了,对此有了以下需求:
需要实现不同的页面对应不同的title。我们可以用 router.beforeEach 来实现

router.js 

{
      path: '/personal',
      component: () => import('../view/home.vue'),
      meta:{
        title:'首页',
      }
    },
    {
      path: '/personal',
      component: () => import('../view/personal.vue'),
      meta:{
        title:'个人中心'
      }
    }


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

但是我们如果是从不同页面点击进来, 显示的title不一样,怎么实现呢?

 created() {
    if(this.$route.query.iswhere=="personal"){
      this.$route.meta.title='商城预览'
    }
}

 但是呢,问题来了。微信公众号顶部的title并没有变过来。如何实现呢?我们可以像在导航钩子里面用window.document.title来实现:

 created() {
    if(this.$route.query.iswhere=="personal"){
      this.$route.meta.title='商城预览'
      window.document.title = '商城预览'
    }
}

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐