最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Provide a fallback icon when using @iconifysvelte for icons that may not be available in the API? - Stack Overflow

programmeradmin5浏览0评论

I am loading symbols dynamically from an API endpoint. I am using iconify/svelte to render icons.

My code looks like this:

<script lang="ts">
    import Icon from 'iconify/svelte';
    
    type NewsItem = {
        id: string;
        link: string;
        title: string;
        tags: Array<{symbol: string; name: string;}></>
    }
    
    const { data } = $props();


    const newsItem: NewsItem = $derived(data.newsItem);

</script>

<main>
    {#if newsItem}

        <div class="news-detail-tags">
            {#each newsItem.tags as newsItemTag (newsItemTag)}
                <a
                    class="news-detail-tag"
                    data-sveltekit-preload-data="off"
                    href={newsItem.link}
                >
                    <Icon icon={'cryptocurrency-color:' + newsItemTag.symbol} />
                    <span class="news-detail-symbol">
                        {newsItemTag.symbol}
                    </span>
                </a>
            {/each}
        </div>
    {/if}
</main>

Symbols that exist such as btc work fine because cryptocurrency-color:btc exists and can be loaded. I often run into symbols that dont exist because the API is not in my control.

Reading the documentation for cryptocurrency-icons it seems they provide a fallback icon that you can use for symbols that don't exist in their iconbase.

How do I specify that a fallback should be rendered?

Something like

<Icon icon={'cryptocurrency-color:' + newsItemTag.symbol} fallback='cryptocurrency-color:generic' />

Their documentation mentions a method called iconExists. However their maintainers claim that it only checks if the icon is in local storage and not on their API

How to provide a fallback icon when you are loading these dynamically?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论