主题
完整的评论区 UI:最新/最热排序、楼中楼预览、点赞、表情、相对时间、IP 归属地、等级与徽章、游客留言。 组件不认接口地址,只认一份 CommentApi 适配器,所以后台(走宿主 request)和服务端直出的文章页(几个 fetch)都能复用同一份 UI。
CommentApi
示例用内存数据实现了一份最小适配器,可以发表、点赞、展开。currentUser 传对象表示已登录。
<script setup lang="ts"> import { ref } from 'vue'; import { KCommentSection } from '@wujies/components'; import type { CommentApi, CommentItem } from '@wujies/components'; const items = ref<CommentItem[]>([ { commentId: 1, content: '这个组件直接吃适配器,不依赖具体后端接口,后台和前台直出都能用。', author: { userId: 2, nickname: '小明', levelName: 'Lv3' }, likeCount: 12, createTime: '2026-12-01 10:00:00', location: '广东', children: [ { commentId: 2, parentId: 1, rootId: 1, content: '楼中楼也支持,默认预览两条。', author: { userId: 3, nickname: '小红' }, likeCount: 3, createTime: '2026-12-01 11:20:00' } ] } ]); const api: CommentApi = { async list() { return items.value; }, async submit(draft) { const item: CommentItem = { commentId: Date.now(), content: draft.content, parentId: draft.parentId, rootId: draft.rootId, author: { userId: 1001, nickname: '文档访客' }, likeCount: 0, createTime: new Date().toISOString() }; items.value.unshift(item); return item; }, async like(commentId, liked) { const target = items.value.find((item) => item.commentId === commentId); if (target) { target.likedByMe = liked; target.likeCount = (target.likeCount || 0) + (liked ? 1 : -1); return target.likeCount; } return 0; } }; const currentUser = { userId: 1001, nickname: '文档访客' }; </script> <template> <KCommentSection target-type="demo" target-id="1" :api="api" :current-user="currentUser" /> </template>
undefined
null
CommentCurrentUser | null
评论
说点什么…
登录后可发表评论
false
2
list(targetType, targetId, contentAuthorId?)
listPage(..., cursor?, size?)
replies(root)
submit(draft)
like(commentId, liked)
TIP
这一份组件有独立的前台 IIFE 构建(build:widget),样式使用 --k-cw-* 外部主题注入点, 不依赖 Vue 与 Element Plus。改过它要补跑 pnpm --filter @wujies/components build:widget。
build:widget
--k-cw-*
pnpm --filter @wujies/components build:widget
KCommentSection 评论区
完整的评论区 UI:最新/最热排序、楼中楼预览、点赞、表情、相对时间、IP 归属地、等级与徽章、游客留言。 组件不认接口地址,只认一份
CommentApi适配器,所以后台(走宿主 request)和服务端直出的文章页(几个 fetch)都能复用同一份 UI。基础用法
示例用内存数据实现了一份最小适配器,可以发表、点赞、展开。currentUser 传对象表示已登录。
评论
还没有评论,来占第一个沙发
API
Attributes
CommentApiundefined= 尚未获知,null= 确认匿名CommentCurrentUser | null评论说点什么…登录后可发表评论false2CommentApi
list(targetType, targetId, contentAuthorId?)listPage(..., cursor?, size?)replies(root)submit(draft)like(commentId, liked)TIP
这一份组件有独立的前台 IIFE 构建(
build:widget),样式使用--k-cw-*外部主题注入点, 不依赖 Vue 与 Element Plus。改过它要补跑pnpm --filter @wujies/components build:widget。