Created
July 15, 2020 06:47
-
-
Save Glutnix/64d768c0e2f780c5247905f4ee4d65ab to your computer and use it in GitHub Desktop.
nuxt + nuxt-composition-api + typescript + @nuxt/content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
nuxt.js + nuxt-composition-api + typescript + @nuxt/content to work with meta generated by $content in setup, | |
and have it generate the appropriate tags at generation time! | |
<journal-post v-if="journal" :post="journal" /> | |
</div> | |
</template> | |
<script lang="ts"> | |
import { | |
defineComponent, | |
useMeta, | |
useContext, | |
computed, | |
useStatic, | |
} from 'nuxt-composition-api' | |
import { Result } from '@nuxt/content'; | |
interface JournalResult extends Result { | |
title: string; | |
description: string; | |
excerpt?: string; | |
}; | |
const journalSlug = defineComponent({ | |
head: {}, | |
setup () { | |
const { params, $content } = useContext(); | |
const slug = computed(() => params.value.slug); | |
const { title, meta } = useMeta(); | |
const journal = useStatic<JournalResult>(async (slug) => { | |
const theJournal = await $content('journals', slug).fetch<JournalResult>(); | |
const theTitle: theJournal.title, | |
const theDescription: theJournal.description ?? theJournal.excerpt ?? undefined, | |
}); | |
title.value = title; | |
meta.value = [ | |
{ hid: 'description', name: 'description', content: description }, | |
]; | |
return theJournal; | |
}, slug, 'journal'); | |
return { | |
journal, | |
} | |
}, | |
}); | |
export default journalSlug; | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
head: { /* ... */ }, | |
pwa: { /* ... */ }, | |
buildModules: [ | |
'nuxt-composition-api', | |
'@nuxt/typescript-build', | |
], | |
modules: [ | |
'nuxt-buefy', | |
'@nuxtjs/pwa', | |
'@nuxt/content', | |
], | |
hooks: { | |
'content:file:beforeInsert': (document) => { | |
if (document.extension === '.md') { | |
document.excerpt = document.text.substring(0, 120); | |
} | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get an error:
Module '"@nuxt/content"' has no exported member 'Result'.
And I went into the nuxt/content repository and didn't find it in the types
Though it still works (what little I've adapted)
EDIT:
Seems like the import should be:
import { Result } from 'nuxt'
EDIT 2:
Just noticed that it's not in nuxt either (can't find an annotation file, still works)