“Duplicate elements in #each Svelte” 可以翻译为 “Svelte 中 #each 中的重复元素”。

huangapple go评论141阅读模式
英文:

Duplicate elements in #each Svelte

问题

您可以通过在循环相关文章时使用一个额外的集合来跟踪已经添加的文章,以避免重复。以下是修改后的代码:

  1. <script>
  2. import { getMarkdownPosts } from '$lib/utils/getPosts';
  3. let posts = getMarkdownPosts();
  4. let addedArticles = []; // 用于跟踪已经添加的文章
  5. export let currentPostTitle, currentPostTags;
  6. function hasArticleBeenAdded(slug) {
  7. return addedArticles.includes(slug);
  8. }
  9. function markArticleAsAdded(slug) {
  10. addedArticles.push(slug);
  11. }
  12. </script>
  13. {#await posts}
  14. Loading...
  15. {:then posts}
  16. Related posts
  17. <ul>
  18. {#each posts as {slug, meta: {title, tags, published}}}
  19. {#if published}
  20. {#if currentPostTitle != title && !hasArticleBeenAdded(slug)}
  21. {#each tags as tag}
  22. <!-- {#if index === 1 } -->
  23. {#if currentPostTags.includes(tag)}
  24. <li><a href="/blog/{slug}"><h4>{title}</h4></a></li>
  25. {#each posts as post}
  26. {#if post.slug === slug}
  27. {#exec markArticleAsAdded(slug)}
  28. {/if}
  29. {/each}
  30. {/if}
  31. <!-- {/if} -->
  32. {/each}
  33. {/if}
  34. {/if}
  35. {/each}
  36. </ul>
  37. {/await}

这样,您将使用 addedArticles 数组来跟踪已经添加的文章的 slug,并且只有当文章未被添加时才添加它们到相关文章列表中。当您添加文章后,使用 markArticleAsAdded(slug) 函数来将文章标记为已添加,以确保不会重复列出相同的文章。

英文:

I have a component on related posts on the Svelte website. It works, but the problem is that it duplicates the articles in the block. That is, for example, if I have three identical tags contained in two articles, then in each of these articles another article in the related posts block will be listed three times.

  1. &lt;script&gt;
  2. import { getMarkdownPosts } from &#39;$lib/utils/getPosts&#39;
  3. let posts = getMarkdownPosts()
  4. export let currentPostTitle, currentPostTags
  5. &lt;/script&gt;
  6. {#await posts}
  7. Loading...
  8. {:then posts}
  9. Related posts
  10. &lt;ul&gt;
  11. {#each posts as {slug, meta: {title, tags, published}}}
  12. {#if published}
  13. {#if currentPostTitle != title}
  14. {#each tags as tag}
  15. &lt;!-- {#if index === 1 } --&gt;
  16. {#if currentPostTags.includes(tag)}
  17. &lt;li&gt;&lt;a href=&quot;/blog/{slug}&quot;&gt;&lt;h4&gt;{title}&lt;/h4&gt;&lt;/a&gt;&lt;/li&gt;
  18. {/if}
  19. &lt;!-- {/if} --&gt;
  20. {/each}
  21. {/if}
  22. {/if}
  23. {/each}
  24. &lt;/ul&gt;
  25. {/await}

How can I change this code so that this does not happen?

答案1

得分: 0

我考虑了一下,重新编写了这个组件。现在它工作得很好。

英文:

I thought about it and rewrote the component. It's working fine now

  1. &lt;script&gt;
  2. import { getMarkdownPosts } from &#39;$lib/utils/getPosts&#39;
  3. import { onMount } from &#39;svelte&#39;
  4. let posts = []
  5. let relatedPosts = []
  6. export let currentPostTitle, currentPostTags
  7. onMount(async () =&gt; {
  8. posts = await getMarkdownPosts()
  9. filterRelatedPosts()
  10. })
  11. function filterRelatedPosts() {
  12. relatedPosts = posts.filter(post =&gt; {
  13. // Exclude the current post and duplicates based on tags
  14. return (
  15. post.meta.title !== currentPostTitle &amp;&amp; post.meta.published &amp;&amp;
  16. currentPostTags.some(tag =&gt; post.meta.tags.includes(tag))
  17. )
  18. })
  19. }
  20. &lt;/script&gt;
  21. {#if relatedPosts.length &gt; 0}
  22. Related posts
  23. &lt;ul&gt;
  24. {#each relatedPosts as { slug, meta: { title } }}
  25. &lt;li&gt;&lt;a href=&quot;/blog/{slug}&quot;&gt;&lt;h4&gt;{title}&lt;/h4&gt;&lt;/a&gt;&lt;/li&gt;
  26. {/each}
  27. &lt;/ul&gt;
  28. {:else}
  29. No related posts found.
  30. {/if}

huangapple
  • 本文由 发表于 2023年8月5日 02:49:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定