Skip to content

Commit 148755a

Browse files
committed
refactor: 简化ApiResponse类型并修复BlogPost组件中的图片路径处理
简化了`ApiResponse`类型定义,直接使用泛型类型`T`代替原有结构。修复了`BlogPost`组件中`featured_image`路径处理的错误,并移除了不必要的注释。同时,添加了`console.log`用于调试文章数据。
1 parent c0e5443 commit 148755a

2 files changed

Lines changed: 5 additions & 9 deletions

File tree

blog-web/src/components/solid/BlogPost.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ async function fetchPost(slug: string): Promise<Post | null> {
3434
withToken: false,
3535
});
3636

37-
// 检查响应格式
3837
if (response && response.id) {
39-
// 处理特殊字符
4038
return {
4139
...response,
4240
featured_image: response.featured_image
43-
? response.data.featured_image.replace(/`/g, "").trim()
41+
? response.featured_image.replace(/`/g, "").trim()
4442
: null,
4543
};
4644
} else {
@@ -53,13 +51,15 @@ async function fetchPost(slug: string): Promise<Post | null> {
5351
}
5452
}
5553

54+
5655
interface BlogPostProps {
5756
slug: string;
5857
}
5958

6059
export default function BlogPost(props: BlogPostProps) {
6160
// 使用SolidJS的资源加载功能获取文章
6261
const [post] = createResource<Post | null>(() => fetchPost(props.slug));
62+
console.log(post());
6363

6464
return (
6565
<div class="container mx-auto px-4 py-8">
@@ -98,7 +98,7 @@ export default function BlogPost(props: BlogPostProps) {
9898
<article class="prose prose-lg max-w-none dark:prose-invert">
9999
{post()!.featured_image && (
100100
<img
101-
src={post()!.featured_image}
101+
src={post()!.featured_image ?? ''}
102102
alt={post()!.title}
103103
class="w-full h-64 md:h-96 object-cover rounded-lg shadow-md mb-8"
104104
/>

blog-web/src/lib/axios.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ interface RequestConfig extends AxiosRequestConfig {
1010
}
1111

1212
// 定义响应数据的通用接口
13-
interface ApiResponse<T = any> {
14-
code: number;
15-
data: T;
16-
message: string;
17-
}
13+
type ApiResponse<T = any> = T;
1814

1915
/**
2016
* HTTP请求类,封装axios实例

0 commit comments

Comments
 (0)