Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic title in composition API #778

Open
idesignzone opened this issue Sep 4, 2022 · 1 comment
Open

Dynamic title in composition API #778

idesignzone opened this issue Sep 4, 2022 · 1 comment

Comments

@idesignzone
Copy link

idesignzone commented Sep 4, 2022

I am trying to get a dynamic title for useMeta with composition API but it does not work.

<script setup>
import { computed } from 'vue'
import { POST } from '@/constants/blog'
import { useQuery, useResult } from "@vue/apollo-composable";
import { useRoute } from 'vue-router'
import { useMeta } from "vue-meta";

  const route = useRoute();
  const variables = computed(() => ({
    slug: route.params.slug,
  }));
  const { result, loading, error } = useQuery(
    POST, variables
  );
  const post = useResult(result, null, data => data.post.data );
  const metaTitle = computed(() => ({
    title: post.attributes.title,
  }));
  useMeta(metaTitle);
  
</script>

Here is the response

{
  "data": {
    "post": {
      "data": {
        "id": 4,
        "attributes": {
          "title": "This is the post title"
        }
      }
    }
  }
}

Please help me understand what is wrong here!

@vlad-grybennikov
Copy link

vlad-grybennikov commented May 30, 2023

I am trying to get a dynamic title for useMeta with composition API but it does not work.

<script setup>
import { computed } from 'vue'
import { POST } from '@/constants/blog'
import { useQuery, useResult } from "@vue/apollo-composable";
import { useRoute } from 'vue-router'
import { useMeta } from "vue-meta";

  const route = useRoute();
  const variables = computed(() => ({
    slug: route.params.slug,
  }));
  const { result, loading, error } = useQuery(
    POST, variables
  );
  const post = useResult(result, null, data => data.post.data );
  const metaTitle = computed(() => ({
    title: post.attributes.title,
  }));
  useMeta(metaTitle);
  
</script>

Here is the response

{
  "data": {
    "post": {
      "data": {
        "id": 4,
        "attributes": {
          "title": "This is the post title"
        }
      }
    }
  }
}

Please help me understand what is wrong here!

You should use the correct computed for this case. Try to add data here

const metaTitle = computed(() => ({
    title: post.data.attributes.title,
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants