在blazor应用中自动注入javascript我正在开发一个Blazor扩展库。该库中的一件事是重用javascriptalert()方法。我知道如何做到这一点,这涉及到在我的.cshtml页面中添加:Blazor.registerFunction('Alert',(message)=>{alert(message);});这在我的代码中:publicvoidAlert(stringmessage){RegisteredFunction.Invoke("Alert",message);如果你使用我的包(或者可能总是),我希望javascript部分以某种方式自动注入到html中。不确定这是否可能(还)有什么想法吗?编辑自blazor0.2以来,有一种更受支持的方法来执行此操作。请参阅blazorlib模板的示例:dotnetnew-iMicrosoft.AspNetCore.Blazor.Templatesdotnetnewblazorlib0.1answer我最终创建了自己的blazor组件,其中包含我的javascript,如下所示:公共类BlazorExtensionScripts:Microsoft.AspNetCore.Blazor。Components.BlazorComponent{protectedoverridevoidBuildRenderTree(Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilderbuilder){builder.OpenElement(0,"script");builder.AddContent(1,"Blazor.registerFunction('Alert',(message)=>{alert(message);});");建造者.CloseElement();并将其添加到我的app.cshtml中,如下所示:以上是C#学习教程:在blazor应用程序中自动注入所有由javascript共享的内容。如果对大家有用,需要了解更多C#学习教程,希望大家多加关注—@addTagHelper*,BlazorExtensions本文收集自网络,不代表立场。如涉及侵权,请点击权利联系管理会员删除。如需转载请注明出处:
