fix(article): 文章列表页收藏状态与详情页同步
修复从文章详情页返回列表页时收藏状态未更新的问题。 - 在 material-list 页面添加 eventBus FAVORITES_UPDATE 事件监听 - 新增 handleFavoriteUpdate 函数处理来自详情页的收藏状态更新 - 使用 onMounted/onUnmounted 管理事件监听器生命周期 - 修复字段映射问题(metaId → id) 影响范围: src/pages/material-list/index.vue Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Showing
1 changed file
with
62 additions
and
4 deletions
| ... | @@ -78,8 +78,8 @@ | ... | @@ -78,8 +78,8 @@ |
| 78 | </template> | 78 | </template> |
| 79 | 79 | ||
| 80 | <script setup> | 80 | <script setup> |
| 81 | -import { ref, computed, watch } from 'vue' | 81 | +import { ref, computed, watch, onMounted, onUnmounted } from 'vue' |
| 82 | -import { useLoad } from '@tarojs/taro' | 82 | +import { useLoad, useDidShow } from '@tarojs/taro' |
| 83 | import NavHeader from '@/components/navigation/NavHeader.vue' | 83 | import NavHeader from '@/components/navigation/NavHeader.vue' |
| 84 | import SearchBar from '@/components/forms/SearchBar.vue' | 84 | import SearchBar from '@/components/forms/SearchBar.vue' |
| 85 | import LoadMoreList from '@/components/list/LoadMoreList' | 85 | import LoadMoreList from '@/components/list/LoadMoreList' |
| ... | @@ -89,6 +89,7 @@ import { listAPI } from '@/api/article' | ... | @@ -89,6 +89,7 @@ import { listAPI } from '@/api/article' |
| 89 | import { mockArticleListAPI } from '@/utils/mockData' | 89 | import { mockArticleListAPI } from '@/utils/mockData' |
| 90 | import Taro from '@tarojs/taro' | 90 | import Taro from '@tarojs/taro' |
| 91 | import { USE_MOCK_DATA } from '@/config/app' | 91 | import { USE_MOCK_DATA } from '@/config/app' |
| 92 | +import eventBus, { Events } from '@/utils/eventBus' | ||
| 92 | 93 | ||
| 93 | // ⚠️ MOCK 数据开关 - 统一从 @/config/app 导入 | 94 | // ⚠️ MOCK 数据开关 - 统一从 @/config/app 导入 |
| 94 | // const USE_MOCK_DATA = process.env.NODE_ENV === 'development' | 95 | // const USE_MOCK_DATA = process.env.NODE_ENV === 'development' |
| ... | @@ -663,14 +664,71 @@ const handleCollectChanged = (item, newStatus) => { | ... | @@ -663,14 +664,71 @@ const handleCollectChanged = (item, newStatus) => { |
| 663 | } | 664 | } |
| 664 | 665 | ||
| 665 | // 更新所有缓存中的收藏状态 | 666 | // 更新所有缓存中的收藏状态 |
| 666 | - categoryListCache.value.forEach((list, key) => { | 667 | + categoryListCache.value.forEach((list) => { |
| 667 | const cachedArticle = list.find(a => a.id === item.id) | 668 | const cachedArticle = list.find(a => a.id === item.id) |
| 668 | if (cachedArticle) { | 669 | if (cachedArticle) { |
| 669 | cachedArticle.collected = newStatus.collected | 670 | cachedArticle.collected = newStatus.collected |
| 670 | } | 671 | } |
| 671 | }) | 672 | }) |
| 672 | } | 673 | } |
| 673 | -</script> | 674 | + |
| 675 | +/** | ||
| 676 | + * 处理来自文章详情页的收藏更新事件 | ||
| 677 | + * | ||
| 678 | + * @description 监听 eventBus 的 FAVORITES_UPDATE 事件,更新列表中的收藏状态 | ||
| 679 | + */ | ||
| 680 | +const handleFavoriteUpdate = (payload) => { | ||
| 681 | + console.log('[Article List] 收到收藏更新事件:', payload) | ||
| 682 | + | ||
| 683 | + // eventBus 事件使用 metaId 字段,需要映射到 id | ||
| 684 | + const metaId = payload.metaId | ||
| 685 | + const collected = payload.collected | ||
| 686 | + | ||
| 687 | + // 更新 allList 中的收藏状态 | ||
| 688 | + const article = allList.value.find(a => a.id === metaId) | ||
| 689 | + if (article) { | ||
| 690 | + article.collected = collected | ||
| 691 | + console.log('[Article List] 已更新 allList 中的收藏状态:', article.title, collected) | ||
| 692 | + } | ||
| 693 | + | ||
| 694 | + // 更新 currentList 中的收藏状态(如果存在) | ||
| 695 | + const currentArticle = currentList.value.find(a => a.id === metaId) | ||
| 696 | + if (currentArticle) { | ||
| 697 | + currentArticle.collected = collected | ||
| 698 | + console.log('[Article List] 已更新 currentList 中的收藏状态:', currentArticle.title, collected) | ||
| 699 | + } | ||
| 700 | + | ||
| 701 | + // 更新所有缓存中的收藏状态 | ||
| 702 | + categoryListCache.value.forEach((list) => { | ||
| 703 | + const cachedArticle = list.find(a => a.id === metaId) | ||
| 704 | + if (cachedArticle) { | ||
| 705 | + cachedArticle.collected = collected | ||
| 706 | + } | ||
| 707 | + }) | ||
| 708 | +} | ||
| 709 | + | ||
| 710 | +/** | ||
| 711 | + * 页面显示时刷新(从其他页面返回时) | ||
| 712 | + */ | ||
| 713 | +useDidShow(() => { | ||
| 714 | + console.log('[Article List] 页面显示') | ||
| 715 | +}) | ||
| 716 | + | ||
| 717 | +/** | ||
| 718 | + * 组件挂载时注册事件监听器 | ||
| 719 | + */ | ||
| 720 | +onMounted(() => { | ||
| 721 | + console.log('[Article List] 注册收藏更新事件监听器') | ||
| 722 | + eventBus.on(Events.FAVORITES_UPDATE, handleFavoriteUpdate) | ||
| 723 | +}) | ||
| 724 | + | ||
| 725 | +/** | ||
| 726 | + * 组件卸载时清理事件监听器 | ||
| 727 | + */ | ||
| 728 | +onUnmounted(() => { | ||
| 729 | + console.log('[Article List] 清理收藏更新事件监听器') | ||
| 730 | + eventBus.off(Events.FAVORITES_UPDATE, handleFavoriteUpdate) | ||
| 731 | +})</script> | ||
| 674 | 732 | ||
| 675 | <style lang="less"> | 733 | <style lang="less"> |
| 676 | // FilterTabs 风格的标签栏 | 734 | // FilterTabs 风格的标签栏 | ... | ... |
-
Please register or login to post a comment