vue頁面title問題(微信公衆號)

一般來說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 = '商城預覽'
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章