vue dynamically modify the title

1, using the routing

router.js
 // add meta data corresponding to the attributes of the route json meta: {title: "Home"} 
{path: ' / index ' , Component: index, meta: {title: " home " },}

 

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

 

2, the method using the directive

// Create command corresponding vue vue this page <div v-title data-title = "{$ title}"> $ title -> title content 
Vue.directive ( ' title ' , { 
  inserted The: function (EL, Binding ) { 
    document.title = el.dataset.title 
  } 
})

 

Guess you like

Origin www.cnblogs.com/cloud-k/p/11464808.html