简体   繁体   English

当我刷新页面时,获取的内容消失了

[英]Fetched content disappears when I refresh the page

whenever I refresh my page the fetched content I've loaded decides to disappear, it will load the first time but every time after that it will go. I have another component that has almost the same code and that one works fine so I'm not entirely sure why it doesn't with this component.每当我刷新我的页面时,我加载的获取内容决定消失,它会第一次加载,但之后每次都会加载 go。我有另一个组件,它的代码几乎相同,而且工作正常,所以我不完全确定为什么它不与此组件一起使用。

the feeling I have is in my standings.svelte component I have a flatMap function which is the main difference compared to my other components.我的感觉是在我的 standings.svelte 组件中,我有一个 flatMap function,这是与其他组件相比的主要区别。

here is a video showing what happens when I refresh the page.这是一段视频,显示了刷新页面时发生的情况。 This won't happen to any other component but this one.这不会发生在任何其他组件上,但不会发生在这个组件上。 ( https://imgur.com/a/Ew4bwgB ) ( https://imgur.com/a/Ew4bwgB )

This is my standings.svelte component这是我的 standings.svelte 组件

<script>
import {leagueStandings} from "../../stores/league-standings-stores"

const tablePositions = $leagueStandings.flatMap(({ standings: { data } }) => data);

</script>

<div class="bg-[#1C1C25] p-8 rounded-lg box-border w-fit">
    

    {#each tablePositions as tablePosition}
            <div class="standings-table flex gap-9 mb-2 pb-4 pt-3 border-b border-[#303041]">
                <div class="team-details flex gap-4 w-full" id="td">
                    <p class="w-[18px]">{tablePosition.position}</p>
                    <img src="{tablePosition.team.data.logo_path}" alt="" class="w-[1.5em] object-scale-down">
                    <p class="">{tablePosition.team_name}</p>
                </div>

                <div class="team-stats flex gap-5 text-left child:w-5 child:text-center w-full">
                    <p>{tablePosition.overall.games_played}</p>
                    <p>{tablePosition.overall.won}</p>
                    <p>{tablePosition.overall.draw}</p>
                    <p>{tablePosition.overall.lost}</p>
                    <p>{tablePosition.overall.goals_scored}</p>
                    <p>{tablePosition.overall.goals_against}</p>
                    <p>{tablePosition.total.goal_difference}</p>
                    <p>{tablePosition.overall.points}</p>
                    <p class="!w-[78px] !text-left">{tablePosition.recent_form}</p>
                </div>
            </div>
    {/each}
</div>

Here is my svelte store这是我的 svelte 商店

import { writable } from "svelte/store";

export const leagueStandings = writable([]);

const fetchStandings = async () => {
    const url = `https://soccer.sportmonks.com/api/v2.0/standings/season/19734?api_token=API_KEY`;
    const res = await fetch(url);
    const data = await res.json();
    leagueStandings.set(data.data);

}
fetchStandings();

id love some advice on what im doing wrong:)我喜欢一些关于我做错了什么的建议:)

Data by default doesn't persist in svelte/store.默认情况下,数据不会保留在 svelte/store 中。 When you transition from one component to another the data get passed on to the child or linked component.当您从一个组件转换到另一个组件时,数据会传递给子组件或链接组件。 But on refreshing the page that data is not accessible since you are directly accessing the child/linked component.但是在刷新页面时无法访问数据,因为您正在直接访问子/链接组件。 To be able to access the data after refresh you need to store them in Browser's localStorage.为了能够在刷新后访问数据,您需要将它们存储在浏览器的 localStorage 中。 You can try this solution https://stackoverflow.com/a/56489832/14018280您可以试试这个解决方案https://stackoverflow.com/a/56489832/14018280

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

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