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.

How to use Nuxt Content with Composition API and TypeScript

See original GitHub issue

Hello, I would like to use Nuxt/Content into my project with the Nuxt-composition-api module and Typescript. I have test many methods to get a content from my directory.

Here is some examples :

setup() {
    const {$content} = useContext()
    const info = useAsync( () => {
      $content('infos').fetch<InfoData>()
    })

    return {
      info
    }
  }

const info = await $content('infos').fetch<InfoData>()

Here is my InfoData object :

interface InfoData {
  age: number
  hiring_status: string,
  hiring_color: string
}

I have errors like "TypeError: Cannot read property 'value' of undefined" or "TypeError: Cannot read property 'age' of undefined"

My infos.json file is :

{
  "age": 18,
  "hiring": {
    "status": "available",
    "color": "green"
  }
}

Do you have an example to use the Nuxt content module with the composition api ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
danielroecommented, Mar 27, 2021

@ArthurDanjou I’ve not tested this, but try:

setup() {
    const { $content } = useContext()
    const info = useAsync(() => {
      return $content('infos').fetch<InfoData>()
    })

    return {
      // note that info starts out `null` and only gets filled when
      // the async fetch resolves - so check it exists (v-if) before
      // accessing it in your template 
      info
    }
  }
1reaction
danielroecommented, Mar 28, 2021

@ArthurDanjou refs are unwrapped in the template (so there is no .value). You can find out more in the Vue 3 docs though note that there are some differences with Vue 2. You should make your condition v-if="info && info.age".

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started with Nuxt + Composition API + TypeScript
Open your terminal and run this command npx create-nuxt-app nuxt-ts-composition-api make sure to select TypeScript, and $axios during the ...
Read more >
Using TypeScript and Composition API with Nuxt and Netlify ...
1 (Optional) Install nuxt-vite · 2 Install nuxt-typescript and ESLINT packages · 3 Add the Composition API package · 4 Fetching Netlify CMS...
Read more >
nuxt + nuxt-composition-api + typescript + @nuxt/content
Embed Embed this gist in your website. ; Share Copy sharable link for this gist. ; Clone via HTTPS Clone with Git or...
Read more >
Nuxt-Comp-API-Content-Module-TypeScript - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
Scaffolding an app with Vue 3, Nuxt, and TypeScript
Also, for convenience, the Nuxt composition API module exports the @vue/composition-api methods and hooks, so you can import directly from @ ...
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