From 509a6fae36bb2bd79a2f539891dfe31bbc3cd514 Mon Sep 17 00:00:00 2001 From: R2m1liA <15258427350@163.com> Date: Sun, 28 Sep 2025 14:18:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(SSR):=20=E5=B0=86=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=B8=BASSR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将CMS数据的获取方式改为Nuxt的useAsyncData - 将加载模式改为渐进式 --- app/pages/index.vue | 121 +++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 53 deletions(-) diff --git a/app/pages/index.vue b/app/pages/index.vue index b11e9dd..03b3947 100644 --- a/app/pages/index.vue +++ b/app/pages/index.vue @@ -1,6 +1,6 @@ @@ -133,47 +144,47 @@ const { getStrapiLocale } = useLocalizations(); const strapiLocale = getStrapiLocale(); - const carouselImages = ref([]); - const recommend_productions = ref([]); - const recommend_solutions = ref([]); - - const pending = ref(true); - - onMounted(async () => { - try { - const response = await findOne('homepage', undefined, { - populate: { - carousel: { - populate: '*', - }, - recommend_productions: { - populate: { - cover: { - populate: '*', - }, - }, - }, - recommend_solutions: { - populate: { - cover: { - populate: '*', - }, + const { data, pending, error } = useAsyncData('homepage', () => + findOne('homepage', undefined, { + populate: { + carousel: { + populate: '*', + }, + recommend_productions: { + 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; } + .carousel-section { + padding: 0; + } + .homepage-carousel .el-carousel__item { width: 100%; height: 33vw;