【发布时间】:2021-04-04 06:18:30
【问题描述】:
在最新的 Visual Studio 2019 中为我的 Blazor 服务器端解决方案创建 Razor 类库时,我看到以下文件:
ExampleJsInterop.cs
public class ExampleJsInterop : IAsyncDisposable
{
private readonly Lazy<Task<IJSObjectReference>> moduleTask;
public ExampleJsInterop(IJSRuntime jsRuntime)
{
moduleTask = new(() => jsRuntime.InvokeAsync<IJSObjectReference>(
"import", "./_content/MyNamespace/exampleJsInterop.js").AsTask());
}
public async ValueTask<string> Prompt(string message)
{
var module = await moduleTask.Value;
return await module.InvokeAsync<string>("showPrompt", message);
}
public async ValueTask DisposeAsync()
{
if (moduleTask.IsValueCreated)
{
var module = await moduleTask.Value;
await module.DisposeAsync();
}
}
}
exampleJsInterop.js
// This is a JavaScript module that is loaded on demand. It can export any number of
// functions, and may import other JavaScript modules if required.
export function showPrompt(message) {
return prompt(message, 'Type anything here');
}
这很有趣。但是,我想做的是use classes instead。我在搜索过的任何链接中都没有看到对此的任何引用,我发现的最接近的 StackOverflow 问题是this one。
是否可以在 JavaScript/浏览器中导出类,然后通过互操作将它们导入 Blazor?如果有,怎么做?
【问题讨论】:
标签: javascript c# blazor blazor-server-side