一个 Razor 页面中的多个 Razor 组件

发布于 2025-01-11 04:53:25 字数 662 浏览 0 评论 0原文

问题如下。有一个 Razor Page 项目(Blazor 的混合体)。其中一个页面在 ServerPrerendered 模式下使用两个 Razor 组件。它们每个的初始化都需要从数据库加载数据。当页面上仅涉及一个组件时,不会有任何问题 - 一切都按预期工作,包括交互性。但如果你同时使用两个组件,就会发生一些难以理解的事情——只有其中一个组件被正确初始化,而且每次都不同。我不知道该看哪里,因为在带有断点的调试模式下,一切都正常。请告诉我,可能有什么问题?

初始化其中一个组件

protected override async Task OnInitializedAsync()
{
    var postRating = await koooirDbContext.PostRatings
        .FirstOrDefaultAsync(f => f.PostId == PostId);
    if (postRating != null)
    {
        current = new CurrentRating
            {
                Rating = postRating.Rating,
                Votes = postRating.Votes
            };
    }
    dataIsLoaded = true;
}

The problem is the following. There is a Razor Page project (a hybrid of Blazor). One of the pages uses two Razor Components in ServerPrerendered mode. Initialization of each of them requires loading data from the database. When only one component on the page is involved, there are no problems - everything works as expected, including interactivity. But if you use two components at the same time, something incomprehensible happens - only one of the components is initialized correctly, each time it is different. I can't figure out where to look, because in debugging mode with breakpoints everything works out correctly. Tell me, please, what could be the catch?

Initializing one of the components

protected override async Task OnInitializedAsync()
{
    var postRating = await koooirDbContext.PostRatings
        .FirstOrDefaultAsync(f => f.PostId == PostId);
    if (postRating != null)
    {
        current = new CurrentRating
            {
                Rating = postRating.Rating,
                Votes = postRating.Votes
            };
    }
    dataIsLoaded = true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两个我 2025-01-18 04:53:25

问题被删除。问题在于共享 DbContext。 解决方案

The question is removed. The problem was sharing DbContext. Solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文