1

so I'm using vue-meta library to add meta tag to my vue project, and now I'm trying to add my meta from API response, this is what i got from API if i did console.log('response.data.data')

0:
meta_tags_content: "my content"
meta_tags_id: 3
meta_tags_properties: "my property"
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get meta_tags_content: ƒ reactiveGetter()
set meta_tags_content: ƒ reactiveSetter(newVal)
get meta_tags_id: ƒ reactiveGetter()
set meta_tags_id: ƒ reactiveSetter(newVal)
get meta_tags_properties: ƒ reactiveGetter()
set meta_tags_properties: ƒ reactiveSetter(newVal)
__proto__: Object
1:
meta_tags_content: "content"
meta_tags_id: 4
meta_tags_properties: "title"
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get meta_tags_content: ƒ reactiveGetter()
set meta_tags_content: ƒ reactiveSetter(newVal)
get meta_tags_id: ƒ reactiveGetter()
set meta_tags_id: ƒ reactiveSetter(newVal)
get meta_tags_properties: ƒ reactiveGetter()
set meta_tags_properties: ƒ reactiveSetter(newVal)
__proto__: Object
length: 2

I'm trying to add the meta_tags_content and meta_tags_properties to my vue meta tag function (https://www.npmjs.com/package/vue-meta), this is my meta tag function

export default {
  name: 'App',
  metaInfo () {
    return {
      title: 'Tiarapot',
      meta: [
        {
          name: 'description',
          content: 'Memang canggih (harus ganti)'
        },
        {
          property: 'og:title',
          content: 'Website Tiarapot (harus ganti)'
        },
        {
          property: 'og:site-name',
          content: 'Tiarapot'
        },
        {
          property: 'og:type',
          content: 'website'
        },
        {
          name: 'robots',
          content: 'index,follow'
        }
      ]
    }
  },
}

and what I'm trying to do is to make it like this

export default {
  name: 'App',
  metaInfo () {
    return {
      title: 'Tiarapot',
      meta: arrayOfConvertedresponse
    }
  },
}

how to convert those response of array to a meta structured object ?

expected result if i console.log(arrayOfConvertedresponse) is

[
    {
    property: "my properties",
    content: "my content"
    },
    {
    property: "title",
    content: "content"
    }
]
2
  • What you've logged at the top of your question appears to be a Vue observable. Could you please log the structure of the data as it is received from your API? Something like console.log(JSON.stringify(response.data, null, 2)) should look nicer Commented Apr 23, 2021 at 1:04
  • Your meta contains both name and property as attributes. So do you want a solution that just tells you about the property name to put? Like name for normal tag and property for Open Graph in your case? Commented Apr 23, 2021 at 1:48

1 Answer 1

1

If you want to use responsive data in metaInfo, you'll need to assign the array to a data property and refer to it in the metaInfo function

export default {
  name: 'App',
  data: () => ({
    arrayOfConvertedresponse: [] // define initial properties
  }),
  metaInfo () {
    return {
      title: 'Tiarapot',
      meta: this.arrayOfConvertedresponse
    }
  },
  async created () {
    // load data from the API
    const response = await axios.get("/api/meta-tags")

    // map response to the appropriate format and assign to your data property
    this.arrayOfConvertedresponse = response.data.data.map(meta => ({
      property: meta.meta_tags_properties,
      content: meta.meta_tags_content
    }))
  }
}

See https://vue-meta.nuxtjs.org/faq/async-action.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.