简体   繁体   English

Svelte Tabs - 不要重新加载/销毁数据

[英]Svelte Tabs - Don't reload / destroy data

I am using a svelte tabs component that is described here:我正在使用此处描述的苗条标签组件:

https://svelte.dev/repl/8e68120858e5322272dc9136c4bb79cc?version=3.5.1 https://svelte.dev/repl/8e68120858e5322272dc9136c4bb79cc?version=3.5.1

Inside two tabs I have the following svelte template code:在两个选项卡中,我有以下苗条的模板代码:

<TabPanel>
    <Filter/>
    <div id='to-filter' class='scroll-container'>
        {#each value.items as item, i}
                <Item 
                itemFilter={item.name}  
                itemDay={item.itemDay} 
                itemMonth={item.itemMonth} 
                itemYear={item.itemYear} 
                itemCity={item.itemCity} 
                itemCountry={item.itemCountry} 
                itemVenue={item.venue}
                itemLink={item.link}
                itemDotw={itemDatesOnly[i].itemDotw}
                />
        {/each}
    </div>
</TabPanel>

Every time I click on the tabs to switch between the data, it seems to reload or rebuild it.每次我单击选项卡在数据之间切换时,似乎都会重新加载或重建它。 How can I adjust the tab component so that it's not destroying the data each time?如何调整选项卡组件,使其不会每次都破坏数据?

Replace the following code block in TabPanel.svelte替换TabPanel.svelte中的以下代码块

{#if $selectedPanel === panel}
    <slot></slot>
{/if}

With something like this:像这样:

<div hidden={$selectedPanel !== panel}>
    <slot></slot>
</div>

This will ensure that all Tab Panels that are not currently active get a hidden attribute, rendering them invisible for the user, but they will be present in the DOM.这将确保所有当前未激活的选项卡面板都获得一个hidden属性,使它们对用户不可见,但它们将存在于 DOM 中。

This should prevent your nested components from being re-rendered, because they are not destroyed and remounted whenever you toggle between tabs.这应该可以防止您的嵌套组件被重新渲染,因为当您在选项卡之间切换时它们不会被破坏和重新安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM