question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement titleTemplate from vue-meta

See original GitHub issue

Environment


  • Operating System: Darwin
  • Node Version: v14.19.0
  • Nuxt Version: 3.0.0-27386975.6e06d2b
  • Package Manager: yarn@1.22.17
  • Bundler: Vite
  • User Config: css
  • Runtime Modules: -
  • Build Modules: -

Reproduction

Attempt to use titleTemplate like Nuxt 2 offers, and observe that it is ignored.

Describe the bug

Following on the issue from https://github.com/nuxt/nuxt.js/issues/10073, it appears that titleTemplate from vue-meta is not implemented in Nuxt 3. It is implemented in Nuxt 2.

I make use of titleTemplate in all of my Nuxt 2 applications, and I’m curious what the official recommended replacement is for this feature. How can I arrange to have my page titles be something like “PageName - ApplicationName” in Nuxt 3? In Nuxt 2, I would set titleTemplate to %s - ApplicationName in the default layout, then set title in each of my pages’ head section of the Options API. In Nuxt 3, there doesn’t appear to be a way to do this without porting this feature forward from Nuxt 2. Happy to be wrong about this 😃

Additional context

No response

Logs

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
command-tabcommented, Mar 21, 2022

Closing this one out.

With useState, a working solution ends up looking like this in a layout:

<script setup>
const pageTitle = useState('pageTitle')
useMeta({
  title: computed(() => `My App Name - ${pageTitle.value}`)
})
</script>

And this on a simple page:

<script setup>
const pageTitle = useState('pageTitle')
pageTitle.value = 'Some Page Title'
</script>

And when using useAsyncData to load external content in a page and using a value from that content in the page title:

<script setup>
const { data } = await useAsyncData('userDetail', () => $fetch('https://api.github.com/users/command-tab'))
const pageTitle = useState('pageTitle')
pageTitle.value = data.value.name
</script>

Doing the equivalent with a store like Pinia instead of Nuxt State would likely be a very similar implementation.

Thanks to @danielroe for all the help! 🙌🏻

2reactions
jiblett1000commented, Mar 7, 2022

@danielroe Will Do 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining metaInfo | Vue Meta
You can define a [keyName] property in any of your components, by default this is metaInfo . See the API for a list...
Read more >
How To Change Metadata in Vue.js with vue-meta
First, to use vue-meta , open your terminal and navigate to your existing Vue project directory. Then, run the following command:.
Read more >
Trying to set Vue Meta page title using string + variable
I've tried setting a variable within data () and using it statically. Doing this should give me My Website: Blog: some title. What...
Read more >
Untitled
Options `vue-meta` allows a few custom options: ```js Vue.use(Meta, ... metaInfo: { // title will be injected into parent titleTemplate title: 'About Us'...
Read more >
Vue-meta NPM - npm.io
<template> ... </template> <script> export default { metaInfo: { title: 'My Example App', titleTemplate: '%s - Yay!', htmlAttrs: { lang: 'en', amp: true...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found