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.
Setup
Copy the reference space, providing the intended structure to follow this guide. Make sure to update the access token.
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
Learn more about fields in the concept.
Also, create an article-overview
content type block and create it as the root story of the “Articles” folder. This block requires no fields.
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
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', {
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="link.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.
Learn more about parameters and filter queries in the Content Delivery API documentation.
Now, the article overview page shows a list of links to all articles.
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>{{ article.title }}</h1>
<StoryblokRichText :doc="article.content" />
</main>
</template>
To render rich text fields, the StoryblokRichText
component provided by the @storyblok/nuxt
module is used.
Learn more about handling rich text in Storyblok in the fields concept and the @storyblok/richtext reference.
When clicking on links present in the article overview page, an article page renders correctly.
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.join('/') : 'home', {
version: 'draft',
resolve_relations: 'featured-articles.articles',
});
</script>
<template>
<StoryblokComponent v-if="story" :blok="story.content" />
</template>
Learn more in the references concept documentation.
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.
Next Part
Internationalization in NuxtPrevious Part
Dynamic Routing in Nuxt