首页 > 解决方案 > How to update HREF after anchor gets clicked in Blazor?

问题描述

Trying to replace HREF upon click on Anchor with Blazor.

So in pseudo;

click => calls function => replaces HREF => triggers open event, but targets new HREF

couldn't make it happen, what should be the syntax?

<a href="javascript: void(0)" @onclick="@(()=> GetCloudFileSignedUrl(context.FileName))">@context.FileName</a>

Side note Please do not reference https://github.com/aspnet/AspNetCore/issues/5545. I'm asking how to replace the HREF value with an external address which is irrelevant to that issue since I'm OK to use javascript: void(0) at this point.

标签: c#razorblazorblazor-server-sideblazor-client-side

解决方案


感谢@daniherrera,我开始寻找正确的地方并在这里找到答案

        [Inject] public NavigationManager NavigationManager { get; set; }
        [Inject] public IJSRuntime JsRuntime { get; set; }
        public async Task NavigateToUrlAsync(string url, bool openInNewTab)
        {
            if (openInNewTab)
            {
                await JsRuntime.InvokeAsync<object>("open", url, "_blank");
            }
            else
            {
                NavigationManager.NavigateTo(url);
            }
        }

推荐阅读