简体   繁体   中英

Vue .js dynamic page title

Trying to change page title depending on URL params in VueJS project.

In router have the folowing path:

 {
   path: '/:location',
   name: 'home_spec',
   components: { default: Home, header: StarterNavbar, footer: StarterFooter },
 }

and pushing location to title also in router:

router.beforeEach((to, from, next) => {
  if (to.params.location){
    document.title = `${to.params.location}`
  }else{
    document.title = "Default title"
  }
  next()
})

in index.html have

 <title><%= htmlWebpackPlugin.options.title %></title>

And it works fine (tab has desireable title ) BUT sourcecode (CTRL+U) has the default title as in the package,js name parameter.

Is it possible to send the first html page with the desirable title in SPA? If the answer is no, what about SEO is it critical for searching robots?

Maybe try another way of setting page title? for example u can do:

  {
   path: '/:location',
   name: 'home_spec',
   components: { default: Home, header: StarterNavbar, 
                 footer:StarterFooter },
   meta: { pageTitle: 'example' }

  router.beforeEach((to, from) => {
   document.title = to.meta.title;
   } )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM