feat(SSR): 将首页调整为SSR

- 将CMS数据的获取方式改为Nuxt的useAsyncData
- 将加载模式改为渐进式
This commit is contained in:
2025-09-28 14:18:27 +08:00
parent ddd4535707
commit 509a6fae36

View File

@ -1,6 +1,6 @@
<template> <template>
<div class="homepage"> <div class="homepage">
<div v-if="!pending" class="carousel"> <section v-if="!pending" class="carousel-section">
<el-carousel <el-carousel
class="homepage-carousel" class="homepage-carousel"
height="auto" height="auto"
@ -8,7 +8,7 @@
arrow="never" arrow="never"
autoplay autoplay
> >
<el-carousel-item v-for="(item, index) in carouselImages" :key="index"> <el-carousel-item v-for="(item, index) in carousel" :key="index">
<div class="carousel-item"> <div class="carousel-item">
<el-image <el-image
class="carousel-image" class="carousel-image"
@ -23,11 +23,17 @@
</div> </div>
</el-carousel-item> </el-carousel-item>
</el-carousel> </el-carousel>
<section class="homepage-section"> </section>
<h2>推荐产品</h2> <section v-else>
<p> <el-skeleton :rows="5" animated />
探索我们的精选产品满足您的各种需求无论是创新技术还是经典设计我们都为您提供优质选择 </section>
</p>
<section class="homepage-section">
<h2>推荐产品</h2>
<p>
探索我们的精选产品满足您的各种需求无论是创新技术还是经典设计我们都为您提供优质选择
</p>
<div v-if="!pending">
<el-carousel <el-carousel
class="recommend-carousel" class="recommend-carousel"
height="auto" height="auto"
@ -72,10 +78,15 @@
</div> </div>
</el-carousel-item> </el-carousel-item>
</el-carousel> </el-carousel>
</section> </div>
<section class="homepage-section"> <div v-else>
<h2>推荐解决方案</h2> <el-skeleton :rows="4" animated />
<p>了解我们的定制解决方案,帮助您优化业务流程,提高效率。</p> </div>
</section>
<section class="homepage-section">
<h2>推荐解决方案</h2>
<p>了解我们的定制解决方案,帮助您优化业务流程,提高效率。</p>
<div v-if="!pending">
<el-carousel <el-carousel
class="recommend-carousel" class="recommend-carousel"
height="auto" height="auto"
@ -120,11 +131,11 @@
</div> </div>
</el-carousel-item> </el-carousel-item>
</el-carousel> </el-carousel>
</section> </div>
</div> <div v-else>
<div v-else class="loading"> <el-skeleton :rows="4" animated />
<el-skeleton :rows="3" animated /> </div>
</div> </section>
</div> </div>
</template> </template>
@ -133,47 +144,47 @@
const { getStrapiLocale } = useLocalizations(); const { getStrapiLocale } = useLocalizations();
const strapiLocale = getStrapiLocale(); const strapiLocale = getStrapiLocale();
const carouselImages = ref<StrapiImage[]>([]); const { data, pending, error } = useAsyncData('homepage', () =>
const recommend_productions = ref<Production[]>([]); findOne<StrapiHomepage>('homepage', undefined, {
const recommend_solutions = ref<Solution[]>([]); populate: {
carousel: {
const pending = ref(true); populate: '*',
},
onMounted(async () => { recommend_productions: {
try { populate: {
const response = await findOne<StrapiHomepage>('homepage', undefined, { cover: {
populate: { populate: '*',
carousel: {
populate: '*',
},
recommend_productions: {
populate: {
cover: {
populate: '*',
},
},
},
recommend_solutions: {
populate: {
cover: {
populate: '*',
},
}, },
}, },
}, },
locale: strapiLocale, recommend_solutions: {
populate: {
cover: {
populate: '*',
},
},
},
},
locale: strapiLocale,
})
);
const carousel = computed(() => data.value?.data.carousel || []);
const recommend_productions = computed(
() => data.value?.data.recommend_productions || []
);
const recommend_solutions = computed(
() => data.value?.data.recommend_solutions || []
);
watch(error, (value) => {
if (value) {
console.error('数据获取失败: ', value);
throw createError({
statusCode: 500,
statusMessage: 'CMS数据获取失败',
fatal: true,
}); });
if (response.data) {
carouselImages.value = response.data.carousel || [];
recommend_productions.value = response.data.recommend_productions || [];
recommend_solutions.value = response.data.recommend_solutions || [];
console.log('推荐产品:', recommend_productions.value);
console.log('推荐解决方案:', recommend_solutions.value);
}
} catch (error) {
console.error('Error fetching homepage data:', error);
} finally {
pending.value = false;
} }
}); });
@ -213,6 +224,10 @@
line-height: 1.6; line-height: 1.6;
} }
.carousel-section {
padding: 0;
}
.homepage-carousel .el-carousel__item { .homepage-carousel .el-carousel__item {
width: 100%; width: 100%;
height: 33vw; height: 33vw;