Content Modeling in Nuxt
Learn how to handle different nestable and content type blocks, render rich text, and use story references to manage content globally.
Alternatively, in the existing space, create a new content type block article
and an “Articles” folder with content. The article content type block should have the following fields:
title
: Textcontent
: Rich text
Create an article-overview
story as a page type content.
Finally, create a featured-articles
nestable block with the following field:
articles
: References
Add a new featured-articles
block to the body
field of the home story and select some articles to be featured.
Fetch and list all articles
Section titled “Fetch and list all articles”Create a new storyblok/ArticleOverview.vue
file to get all stories from this new content type.
<script setup> const storyblokApi = useStoryblokApi(); const articles = await storyblokApi.getAll('cdn/stories', { api: { version: 'draft', starts_with: 'articles', content_type: 'article', }, });</script>
<template> <main> <h1>Article Overview</h1> <article v-for="article in articles" :key="article.uuid"> <NuxtLink :href="article.full_slug">{{ article.content.title }}</NuxtLink> </article> </main></template>
Using the starts_with
parameter, only stories from the “Articles” folder are fetched. Using the content_type
parameter, the results are restricted to stories of the content type article
. This prevents the article-overview
from being included.
Now, the article overview page shows a list of links to all articles.
Create the article block
Section titled “Create the article block”Add a new storyblok/Article.vue
component to render the new article content type.
<script setup>defineProps({ blok: Object });</script>
<template> <main v-editable="blok"> <h1>{{ blok.title }}</h1> <StoryblokRichText :doc="blok.content" /> </main></template>
To render rich text fields, the StoryblokRichText
component provided by the @storyblok/nuxt
module is used.
When clicking on links present in the article overview page, an article page renders correctly.
Handle referenced stories
Section titled “Handle referenced stories”In the pages/[...slug].vue
data file, use the resolve_relations
parameter to receive the complete story object for referenced stories.
<script setup>const slug = useRoute().params.slug;
const story = await useAsyncStoryblok( slug && slug.length > 0 ? slug.join('/') : 'home', { api: { version: 'draft', resolve_relations: 'featured-articles.articles', }, },);</script>
<template> <StoryblokComponent v-if="story" :blok="story.content" /></template>
Next, create a new storyblok/FeaturedArticles.vue
component.
<script setup>defineProps({ blok: Object });</script>
<template> <section v-editable="blok"> <h2>Featured Articles</h2> <article v-for="article in blok.articles" :key="article.uuid"> <NuxtLink :href="article.full_slug">{{ article.content.title }}</NuxtLink> </article> </section></template>
Now, this component will render links to the featured articles on the home page of the project.
Related resources
Section titled “Related resources”Get in touch with the Storyblok community