feat(SSR): 将解决方案页改为SSR

- 使用Nuxt的useAsyncData
- 添加i18n信息
This commit is contained in:
2025-09-28 16:11:35 +08:00
parent dbd9346362
commit a7a4551528
4 changed files with 92 additions and 69 deletions

View File

@ -1,5 +1,6 @@
<template> <template>
<div class="page-container"> <div class="page-container">
<div v-if="!pending">
<div v-if="solution"> <div v-if="solution">
<div class="page-header"> <div class="page-header">
<el-breadcrumb class="breadcrumb" separator="/"> <el-breadcrumb class="breadcrumb" separator="/">
@ -23,7 +24,8 @@
<h1>{{ solution.title }}</h1> <h1>{{ solution.title }}</h1>
<div class="solution-meta"> <div class="solution-meta">
<span class="solution-date"> <span class="solution-date">
CreatedAt: {{ new Date(solution.createdAt).toLocaleDateString() }} CreatedAt:
{{ new Date(solution.createdAt).toLocaleDateString() }}
</span> </span>
</div> </div>
</div> </div>
@ -33,6 +35,20 @@
</div> </div>
</div> </div>
</div> </div>
<div v-else class="not-found">
<el-result
icon="warning"
:title="$t('solution-not-found')"
:sub-title="$t('solution-not-found-desc')"
>
<template #extra>
<el-button type="primary" @click="$router.push('/productions')">
{{ $t('back-to-solutions') }}
</el-button>
</template>
</el-result>
</div>
</div>
<div v-else class="loading"> <div v-else class="loading">
<el-skeleton :rows="5" animated /> <el-skeleton :rows="5" animated />
</div> </div>
@ -45,27 +61,23 @@
const { getStrapiLocale } = useLocalizations(); const { getStrapiLocale } = useLocalizations();
const strapiLocale = getStrapiLocale(); const strapiLocale = getStrapiLocale();
const solution = ref<Solution | null>(null);
// 获取路由参数(documentId) // 获取路由参数(documentId)
const documentId = computed(() => route.params.slug as string); const documentId = computed(() => route.params.slug as string);
onMounted(async () => { const { data, pending, error } = useAsyncData(
try { () => `solution-${documentId.value}`,
const response = await findOne<Solution>('solutions', documentId.value, { () =>
findOne<Solution>('solutions', documentId.value, {
populate: '*', populate: '*',
locale: strapiLocale, locale: strapiLocale,
}); })
if (response.data) { );
solution.value = {
...response.data, const solution = computed(() => data.value?.data ?? null);
// 确保 solution_type 保持原始类型
solution_type: response.data.solution_type, watch(error, (value) => {
}; if (value) {
} console.error('数据获取失败: ', value);
console.log('Fetched Solution:', solution.value);
} catch (error) {
console.error('Failed to fetch solution:', error);
} }
}); });
</script> </script>
@ -127,4 +139,11 @@
align-items: center; align-items: center;
margin-top: 1rem; margin-top: 1rem;
} }
.not-found {
display: flex;
justify-content: center;
align-items: center;
min-height: 400px;
}
</style> </style>

View File

@ -15,7 +15,7 @@
</el-breadcrumb-item> </el-breadcrumb-item>
</el-breadcrumb> </el-breadcrumb>
</div> </div>
<div class="solutions-container"> <div v-if="!pending" class="solutions-container">
<el-tabs v-model="activeName" class="solutions-tabs"> <el-tabs v-model="activeName" class="solutions-tabs">
<el-tab-pane :label="$t('all')" name="all"> <el-tab-pane :label="$t('all')" name="all">
<div class="solution-list"> <div class="solution-list">
@ -48,18 +48,34 @@
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
</div> </div>
<div v-else>
<el-skeleton :rows="6" animated />
</div>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
const { find } = useStrapi(); const { find } = useStrapi();
const { getStrapiLocale } = useLocalizations(); const { getStrapiLocale } = useLocalizations();
const strapiLocale = getStrapiLocale(); const strapiLocale = getStrapiLocale();
const { data, pending, error } = useAsyncData('solutions', () =>
find<Solution>('solutions', {
populate: {
cover: {
populate: '*',
},
solution_type: {
populate: '*',
},
},
locale: strapiLocale,
})
);
const activeName = ref<string>('all'); const activeName = ref<string>('all');
const solutions = ref<Solution[]>([]); const solutions = computed(() => data.value?.data ?? []);
// 按类型分组 // 按类型分组
const groupedSolutions = computed(() => { const groupedSolutions = computed(() => {
@ -81,27 +97,9 @@
return gourps; return gourps;
}); });
onMounted(async () => { watch(error, (value) => {
try { if (value) {
const response = await find<Solution>('solutions', { console.error('数据获取失败: ', value);
populate: {
cover: {
populate: '*',
},
solution_type: {
populate: '*',
},
},
locale: strapiLocale,
});
solutions.value = response.data.map((item: Solution) => ({
...item,
solution_type: item.solution_type,
}));
console.log('Fetched Solutions:', solutions.value);
console.log('Grouped Solutions:', groupedSolutions.value);
} catch (error) {
console.error('Failed to fetch solutions:', error);
} }
}); });
</script> </script>

View File

@ -51,6 +51,9 @@
"product-not-found": "Product Not Found", "product-not-found": "Product Not Found",
"product-not-found-desc": "Sorry, the product you are looking for does not exist or has been removed.", "product-not-found-desc": "Sorry, the product you are looking for does not exist or has been removed.",
"back-to-productions": "Back to Products", "back-to-productions": "Back to Products",
"solution-not-found": "Solution Not Found",
"solution-not-found-desc": "Sorry, the solution you are lokking for does not exist or has been removed.",
"back-to-solutions": "Back to Solutions",
"no-content-available": "No detailed information available", "no-content-available": "No detailed information available",
"loading": "Loading...", "loading": "Loading...",
"our-productions": "Our Productions", "our-productions": "Our Productions",

View File

@ -51,6 +51,9 @@
"product-not-found": "产品未找到", "product-not-found": "产品未找到",
"product-not-found-desc": "抱歉,您访问的产品不存在或已被删除。", "product-not-found-desc": "抱歉,您访问的产品不存在或已被删除。",
"back-to-productions": "返回产品列表", "back-to-productions": "返回产品列表",
"solution-not-found": "解决方案未找到",
"solution-not-found-desc": "抱歉,您访问的解决方案不存在或已被删除",
"back-to-solutions": "返回解决方案列表",
"no-content-available": "暂无详细信息", "no-content-available": "暂无详细信息",
"loading": "加载中...", "loading": "加载中...",
"our-productions": "我们的产品", "our-productions": "我们的产品",