当URL查询更改时,将炸弹式组件强制为rerender
我阅读的所有内容都指出了我使用NavigationManager.location Changed事件,但是当仅查询字符串更改时,这不会检测到。因此,什么都没有开火。
我已经尝试了建议的内容在这里,这并不是我想要的。
我需要我的组件每次更改当前路由中的查询参数。有人可以这样做吗?
更新:
这是我的代码示例:
@page "/accounts"
@if (Accounts != null)
{
<table>
<tbody>
@foreach (var account in Accounts)
{
<tr>
<td>
<NavLink href="/accounts/?accountId={account.AccountId}">@Account.Name</NavLink>
</td>
</tr>
}
</tbody>
</table>
}
@if (Account != null)
{
<div>
<h2>@Account.Name</h2>
</div>
}
@implements IDisposable
@inject NavigationManger NavManager
@inject AccountService AccountService
@code {
protected Guid Id { get; set; } = Guid.NewGid();
protected Guid AccountId { get; set; } = Guid.Empty;
protected List<AccountModel> Accounts { get; set; }
protected AccountModel Account { get; set; }
protected override void OnInitialized()
{
NavManager.LocationChanged += LocationChanged;
}
protected async override Task OnParametersSetAsync()
{
if (AccountId != Guid.Empty)
{
Account = await AccountService.GetAsync(AccountId);
}
else
{
Accounts = await AccountService.GetAllAsync();
}
StateHasChanged();
}
void LocationChanged(object sender, LocationChangedEventArgs e)
{
Id = Guid.NewGuid();
StateHasChanged();
}
void IDisposable.Dispose()
{
NavManager.LocationChanged -= LocationChanged;
}
}
此处的问题是location changed(对象,位置changedeventargs)
在仅在查询参数更改时不会发射。这很有意义,路由是相同的/accounts
。
从基础上讲,我有一个页面显示帐户列表并查看一个帐户。我希望路由为/Account
当查看列表和'/Accounts?accountiD = someaccountid'时,我是在查看一个路线时。使用标准的NAVLink组件不会触发组件的启用者,并且位置改变事件也不会触发。甚至没有设置新的ID值。查看stateHaschanged
仅在值更改时才能重新读取组件。
Everything I've read points me to using NavigationManager.LocationChanged event, however that does not detect when only the query string changes. Therefore, nothing fires.
I've tried what is suggested here and that does not do what I am looking for.
I need my component to rerender everytime the query parameters in the current route changes. Has anyone been able to do this?
UPDATE:
Here is my code example:
@page "/accounts"
@if (Accounts != null)
{
<table>
<tbody>
@foreach (var account in Accounts)
{
<tr>
<td>
<NavLink href="/accounts/?accountId={account.AccountId}">@Account.Name</NavLink>
</td>
</tr>
}
</tbody>
</table>
}
@if (Account != null)
{
<div>
<h2>@Account.Name</h2>
</div>
}
@implements IDisposable
@inject NavigationManger NavManager
@inject AccountService AccountService
@code {
protected Guid Id { get; set; } = Guid.NewGid();
protected Guid AccountId { get; set; } = Guid.Empty;
protected List<AccountModel> Accounts { get; set; }
protected AccountModel Account { get; set; }
protected override void OnInitialized()
{
NavManager.LocationChanged += LocationChanged;
}
protected async override Task OnParametersSetAsync()
{
if (AccountId != Guid.Empty)
{
Account = await AccountService.GetAsync(AccountId);
}
else
{
Accounts = await AccountService.GetAllAsync();
}
StateHasChanged();
}
void LocationChanged(object sender, LocationChangedEventArgs e)
{
Id = Guid.NewGuid();
StateHasChanged();
}
void IDisposable.Dispose()
{
NavManager.LocationChanged -= LocationChanged;
}
}
The problem here is LocationChanged(object, LocationChangedEventArgs)
does not fire when only the query parameters change. Which makes sense, the route is the same /accounts
.
Bascically I have one page to show a list of Accounts and view a single account. I want the route to be /accounts
when view the list and '/accounts?accountId=someAccountId' to be the route when im viewing one. Using standard NavLink component does not trigger a rerender of the component and neither does the LocationChanged event. Not even with setting a new Id value. Seeing as to how StateHasChanged
only rerenders the component if a value has changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终得到了这样的工作,就像这样的工作:
我最终只调用了我想手动在第一个渲染上运行的所有代码。我希望(错误地假设)我可以告诉它,通过更改一个本地属性,然后调用
statehashaschanged
,可以自动从顶部重新读成分生命周期。现在,当我在
“/account”
和“/account?enackid = someid” 的“/accode>”之间切换时。
I ended up getting what I wanted to happen work like this:
I ended up just calling all the same code I want to run on a first render manually. I was hoping (falsely assuming) that I could tell it to just rereun the component lifecycle from the top automatically by changing one local property and then calling
StateHasChanged
.Now when I toggle between
"/accounts"
and"/accounts?accountId=someId"
the component rerenders as I want.我在WASM应用程序中这样做,可以很好地
将其设置在您要接收路由参数的页面上,
然后
将重新渲染任何数据更改,AFAIK您不能强迫组件重新渲染而不重新渲染它,只有更改的订户,这是有道理的,因为重新汇款很昂贵。
I do this in a wasm app works well
set this on your page where you want to receive the route parameter
then
The will re-render any data changes, AFAIK you can't force a component to re-render without reloading it, only the changed subscribers, which makes sense, as re-renders are expensive.
Doc's look under router parameters
您可以将stateHaschanged()绑定到更改查询字符串的函数/逻辑,从而重新渲染组件。
You can bind the StateHasChanged() to the function/logic which changes your query string, which will re-render your component.