简体   繁体   English

如何在 .razor.cs 文件后面注入 blazor 代码? 以 IJSRuntime 为例

[英]How to inject in blazor code behind .razor.cs file? IJSRuntime for example

With a normal single page razor component in Blazor.在 Blazor 中使用普通的单页剃刀组件。 I can inject IJSRuntime like this at the top of the page:我可以像这样在页面顶部注入IJSRuntime

@inject IJSRuntime JSRuntime

If I create a code behind .razor.cs file for the component, how do I inject something like IJSRuntime into the code behind file?如果我为组件在 .razor.cs 文件后面创建了一个代码,我如何将IJSRuntime之类的IJSRuntime注入到代码隐藏文件中?

In the code behind razor.cs file, IJSRunTime or others can be injected with the [Inject] attribute在razor.cs文件后面的代码中,可以使用[Inject]属性注入IJSRunTime或其他

public partial class BillingDashboard
{
    [Inject]
    IJSRuntime JSRuntime { get; set; }

    protected override async Task MyFunction()
    {
         await JSRuntime.InvokeVoidAsync("console.log('test')");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#、Blazor、VS:如何抑制所有 *.razor.cs 和 *.cshtml.cs 文件的 IDE0051 和 IDE0052? - C#, Blazor, VS: How to suppress IDE0051 and IDE0052 for all *.razor.cs and *.cshtml.cs files? blazor 依赖注入差异在.razor 中比在代码后面.cs - blazor dependency injection difference in .razor than in code behind .cs function 在.razor.cs 中覆盖错误,但在.razor 中没有 - function override error in .razor.cs, but not in .razor 如何使用 Blazor 从 .CS 文件而不是 .RAZOR 文件访问在 Startup.cs 中注释的范围服务中的数据? - How do I access data within a scoped service, annotated in Startup.cs, from a .CS file NOT a .RAZOR file using Blazor? 如何在.cs文件中调用在后面的代码中实现的方法? - How to call a method in .cs file which was implemented in code behind? 如何在文件(xaml.cs)后面的代码之外访问样式? - How to access a Style outside the code behind file (xaml.cs)? 如何在asp.net中的CS代码中包含javascript文件 - How to include javascript file in the cs code behind in asp.net 如何从类文件(.cs)文件调用函数到文件(.aspx.cs)之后的另一个代码? - How to call function from class file (.cs) file to another code behind file (.aspx.cs)? 如何在 Razor 文件中正确注入服务? - How to inject a service in a Razor file properly? 替代`IJSRuntime.Current`。 即如何在从 Blazor Web 程序集应用程序引用的库中获取“IJSRuntime” - Alternative to `IJSRuntime.Current`. i.e. How to get `IJSRuntime` in a library referenced from a Blazor Web Assembly application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM