vue動態修改title


1、利用路由

router.js
//在對應路由json數據添加 meta屬性 如meta:{title:"首頁"}
{path: '/index',component: index,meta:{title:"首頁"},}

 

main.js
//利用router方法beforeEach
router.beforeEach((to,from,next)=>{
  
  if(to.meta.title){
    document.title = to.meta.title
  }
  next()
})

 

2、利用directive方法

//新建vue指令 在對應的vue頁面添加 <div v-title data-title="{$title}"> $title --> 標題內容
Vue.directive('title', {
  inserted: function (el, binding) {
    document.title = el.dataset.title
  }
})

 


免責聲明!

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



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