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

查看:27
本文介绍了如何停止 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)"上放置一个断点;单击按钮时,它会调用不执行任何操作的单击事件(代码已注释掉),然后它会重新呈现页面并命中断点.如果我在单击事件中添加代码调用 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

推荐答案

我想知道你是怎么解决这个问题的.我遇到了完全相同的问题(我只想 NavigateTo 而不进行任何重新渲染).覆盖 ShouldRender 返回 false,停止一切 - 即它没有 NavigateTo URL.然而,我导航到同一页面,但在 QS 中有不同的参数.当我在 NavigateTo 方法(public void NavigateTo(string uri, bool forceLoad = false);)中发现一个可选参数时,我似乎已经解决了这个问题(我认为!).我将其设置为 true,现在似乎可以了.只是对你的经历感兴趣

I was wondering how you got on with this. I had exactly the same problem (I just wanted to NavigateTo without any re-rendering). Overriding ShouldRender to return false, stopped everything - i.e. it didn't NavigateTo the URL. I was however, Navigating to the same page, but with different parameters in the QS. I seem to have solved the problem, (I think !) when I discovered an optional parameter in the NavigateTo method (public void NavigateTo(string uri, bool forceLoad = false);). I set that to true and it seems to be OK now. Was just interested in your experience

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

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