head icon indicating copy to clipboard operation
head copied to clipboard

How to use dynamic values?

Open Fredrik-sfo opened this issue 3 years ago • 2 comments

Hi, Probably not an issue but testing this out and can not figure out how add dynamic values in meta.

Base example:

import { defineComponent, computed, reactive } from 'vue'
import { useHead } from '@vueuse/head'

export default defineComponent({
  setup() {
    const siteData = reactive({
      title: `My website`,
      description: `My beautiful website`,
    })

    useHead({
      // Can be static or computed
      title: computed(() => siteData.title),
      meta: [
        {
          name: `description`,
          content: computed(() => siteData.description),
        },
      ],
    })
  },
})

How can you add a variable in title, like:

import { defineComponent, computed, reactive } from 'vue'
import { useHead } from '@vueuse/head'

export default defineComponent({
  setup() {
    const siteData = reactive({
      title: this.title, // Here
      description: `My beautiful website`,
    })

    useHead({
      // Can be static or computed
      title: computed(() => siteData.title),
      meta: [
        {
          name: `description`,
          content: computed(() => siteData.description),
        },
      ],
    })
  },
})

Fredrik-sfo avatar Feb 17 '22 15:02 Fredrik-sfo

What is this.title meant to refer to?

danielroe avatar Feb 18 '22 17:02 danielroe

Ah, sorry, it's a value from a Json response called from a fetch().

Fredrik-sfo avatar Feb 18 '22 21:02 Fredrik-sfo

You'll need to fetch data in the setup function to be able to access it for useHead.

Going to close this issue off, if you still need help feel free to re-open with a full code example

harlan-zw avatar Sep 19 '22 03:09 harlan-zw