28
loading...
This website collects cookies to deliver better user experience
Introduction
Enabling preview mode in Nuxt (> v2.13)
Previewing brand new pages
Generating Posts in Strapi
Conclusion
Basic knowledge of JavaScript
Basic knowledge of Vue.js
Basic knowledge of Nuxt.js
Basic understanding of Strapi — get started here.
// plugins/preview.client.js
export default function ({ query, enablePreview }) {
if (query.preview) {
enablePreview()
}
}
export default {
//... plugins: ['~/plugins/preview.client.js'] //...
}
validate({ params, query }) {
if (query.preview) {
return true
}
<template>
<div>
<section>
<div class="container">
<h3 class="mt-5 text-center">Top Rated Posts</h3>
<p>A list of our highly rated posts.</p>
<Posts class="text-left" />
</div>
</section>
</div>
</template>
<script>
export default {}
</script>
// /components/Posts.vue<template>
<div class="row row-cols-1 row-cols-md-3 g-4">
<Post v-for="post in posts" :key="post.id" :post="post" />
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
computed: {
...mapState({
posts: (state) => {
return state.posts
},
}),
},
}
</script>
<style></style>
<template>
<div class="col">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">{{ post.Title }}</h5>
<p class="card-text">
{{ post.description }}
</p>
<nuxt-link :to="`/posts/${post.id}`" class="btn btn-primary text-white"
>View Post</nuxt-link
>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
post: {
type: Object,
default: () => {},
},
},
}
</script>
<style scoped>
a {
color: inherit;
}
</style>
// store/index.js
export const state = () => ({
posts: [],
})
export const mutations = {
STORE_POSTS(state, posts) {
state.posts = posts
},
}
export const actions = {
async nuxtServerInit({ commit }) {
const posts = await this.$axios
.$get('http://localhost:1337/posts')
.catch((e) => console.error(e))
commit('STORE_POSTS', posts)
},
}