如何停止 blazor @onclick 重新渲染页面 [英] How do I stop blazor @onclick re-rendering page

查看:26
本文介绍了如何停止 blazor @onclick 重新渲染页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 blazor 服务器应用

I am using a blazor sever app

我有一个控件,当使用@onclick 事件处理程序单击时,我想使用单击事件方法中的 NavigationManager 导航到新页面.控件是什么(按钮、a、tr 等)并不重要,它们都具有相同的行为

I have a control that when clicked using the @onclick event handler I want to navigate to a new page using the NavigationManager in the click event method. it doesn't really matter what the control is (button, a, tr, etc) they all have the same behavior

如果我在 HTML 中放置一个断点,我可以看到当前页面在进入新页面之前正在重新呈现.

if I put a break point in the HTML I can see the current page is re-rendering before it goes to the new page.

重现此行为的一种简单方法是创建一个新的 blazor 项目并在 counter.razor 页面中把代码改成这样

a simple way to reproduce this behavior is to make a new blazor project and in the counter.razor page change the code to this

@page "/counter"

<h1>Counter</h1>

@if (1 == 1)
{
<p>Current count: @currentCount</p>
}

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

@code {
private void IncrementCount()
    {
        //currentCount++;
    }
}

在 HTML 行@if (1 == 1)"上放置一个断点;单击按钮时,它会调用不执行任何操作(代码已注释掉)的 click 事件,然后重新渲染页面并命中断点.如果我在点击事件中添加代码调用 navigationManager 以离开页面,则会发生同样的情况,当页面上没有任何更改时,它会在离开页面之前重新呈现.

put a break point on the HTML line "@if (1 == 1)" when the button is clicked it calls the click event which does nothing (code commented out), it then re-renders the page and the break point is hit. The same happens if I add code in the click event calling navigationManager to navigate away from the page, it re-renders before it leaves the page when nothing has changed on the page.

添加 onclick:preventDefault 和/或 @onclick:stopPropagation 不会改变这一点

adding onclick:preventDefault and/or @onclick:stopPropagation does not change this

我发现唯一有效的是添加

the only thing I have found that does work is adding

    private bool c_blnStopRendering = false;

    protected override bool ShouldRender()
    {
        if (c_blnStopRendering == true) { return false; }
        return base.ShouldRender();
    }

并设置 c_blnStopRendering = true;在点击事件中但这似乎过度杀戮并且非常手动地将其添加到任何需要的地方

and setting the c_blnStopRendering = true; in the click event but this seems like over kill and very manual to add it everywhere it is needed

推荐答案

这是目前唯一的方法.backlog中有一个请求https://github.com/dotnet/aspnetcore/issues/18919

That's the only way to do it at the moment. There is a request in the backlog https://github.com/dotnet/aspnetcore/issues/18919

这篇关于如何停止 blazor @onclick 重新渲染页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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