页面为什么不保留数据? [英] Why doesnt the page preserve the data?

查看:75
本文介绍了页面为什么不保留数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我当前正在查看Blazor示例项目.计数器"更具体,当我增加页面上的值,然后单击Web应用程序上的其他选项卡时,假设获取"数据"标签,该标签拉起了天气,然后我又回过头来,增加的值又回到了0.为什么呢?为什么不保存?

So I'm currently looking at the Blazor example project.. The "Counter" to be more specific, and when I increment the value on the page, and then click a different tab on the webapp, let's say the "Fetch data" tab which pulls up the weather, and then I go back, the value that was incremented is back to 0.. Why is that? Why isn't it saved?

这是导航菜单

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
    <ul class="nav flex-column">
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                <span class="oi oi-home" aria-hidden="true"></span> Home
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="counter">
                <span class="oi oi-plus" aria-hidden="true"></span> Counter
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="fetchdata">
                <span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
            </NavLink>
        </li>
    </ul>
</div>

还有柜台

@page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

推荐答案

这是设计使然.当您的计数器页面组件超出范围时,它将被销毁,而当您返回时,它将被重新创建.但是您可以缓存其状态(计数值)并在返回页面时读取最后一个值.您可以将状态值保存在本地存储或会话存储(JavaScript Api)中,可以直接使用Blazor的JSInterop功能使用状态值,或者更好的是,使用库设计来与Blazor社区创建的Blazor一起使用,甚至通过开拓者队(推荐)

This is by design. When your Counter Page Component goes out of scope it is destroyed, and when you go back, it is re-created. But you can cache its state (the count value) and read the last value when you go back to the page. You may save the state value in the local storage or session storage (JavaScript Api), which you can use directly employing the JSInterop feature of Blazor, or still better, use libraries design to work with Blazor created by the Blazor community, and even by the Blazor team (recommended)

希望这对您有帮助...

Hope this helps...

这篇关于页面为什么不保留数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆