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

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

Duplicate elements in #each Svelte

问题

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

<script>
    import { getMarkdownPosts } from '$lib/utils/getPosts';

    let posts = getMarkdownPosts();
    let addedArticles = []; // 用于跟踪已经添加的文章

    export let currentPostTitle, currentPostTags;

    function hasArticleBeenAdded(slug) {
        return addedArticles.includes(slug);
    }

    function markArticleAsAdded(slug) {
        addedArticles.push(slug);
    }
</script>

{#await posts}
    Loading...
{:then posts}
   Related posts
    <ul>
        {#each posts as {slug, meta: {title, tags, published}}}
            {#if published}
                {#if currentPostTitle != title && !hasArticleBeenAdded(slug)}
                    {#each tags as tag}
                        <!-- {#if index === 1 } -->
                            {#if currentPostTags.includes(tag)}
                                <li><a href="/blog/{slug}"><h4>{title}</h4></a></li>
                                {#each posts as post}
                                    {#if post.slug === slug}
                                        {#exec markArticleAsAdded(slug)}
                                    {/if}
                                {/each}
                            {/if}
                        <!-- {/if} -->
                    {/each}
                {/if}
            {/if}
        {/each}
    </ul>
{/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.

&lt;script&gt;
    import { getMarkdownPosts } from &#39;$lib/utils/getPosts&#39;

    let posts = getMarkdownPosts()

    export let currentPostTitle, currentPostTags

&lt;/script&gt;

{#await posts}
    Loading...
{:then posts}
   Related posts
    &lt;ul&gt;
        {#each posts as {slug, meta: {title, tags, published}}}
            {#if published}
                {#if currentPostTitle != title}
                    {#each tags as tag}
                        &lt;!-- {#if index === 1 } --&gt;
                            {#if currentPostTags.includes(tag)}
                                &lt;li&gt;&lt;a href=&quot;/blog/{slug}&quot;&gt;&lt;h4&gt;{title}&lt;/h4&gt;&lt;/a&gt;&lt;/li&gt;
                            {/if}
                        &lt;!-- {/if} --&gt;
                    {/each}
                {/if}
            {/if}
        {/each}
    &lt;/ul&gt;
{/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

&lt;script&gt;
    import { getMarkdownPosts } from &#39;$lib/utils/getPosts&#39;
    import { onMount } from &#39;svelte&#39;

    let posts = []
    let relatedPosts = []

    export let currentPostTitle, currentPostTags

    onMount(async () =&gt; {
      posts = await getMarkdownPosts()
      filterRelatedPosts()
    })

    function filterRelatedPosts() {
      relatedPosts = posts.filter(post =&gt; {
        // Exclude the current post and duplicates based on tags
        return (
          post.meta.title !== currentPostTitle &amp;&amp; post.meta.published &amp;&amp;
          currentPostTags.some(tag =&gt; post.meta.tags.includes(tag))
        )
      })
    }
  &lt;/script&gt;

  {#if relatedPosts.length &gt; 0}
    Related posts
    &lt;ul&gt;
      {#each relatedPosts as { slug, meta: { title } }}
        &lt;li&gt;&lt;a href=&quot;/blog/{slug}&quot;&gt;&lt;h4&gt;{title}&lt;/h4&gt;&lt;/a&gt;&lt;/li&gt;
      {/each}
    &lt;/ul&gt;
  {:else}
    No related posts found.
  {/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:

确定