Код: Выделить всё
// This example if from another topics.
// The pats mentioned here are not related with current topic.
@inject IJSRuntime JS
@code {
private IJSObjectReference? module;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
// Path: _content/[LibraryName]/[PathUnderWwwroot]
module = await JS.InvokeAsync(
"import", "./_content/MySharedLibrary/js/myScript.js");
}
}
Код: Выделить всё
public class JavaScriptFunctionality : IAsyncDisposable
{
private Microsoft.JSInterop.IJSObjectReference? MyLib;
public static JavaScriptFunctionality GetNotInitializedYetInstance()
{
return new JavaScriptFunctionality();
}
public async System.Threading.Tasks.Task Load(
Microsoft.JSInterop.IJSRuntime javaScriptRuntime
)
{
this.MyLib = await javaScriptRuntime.InvokeAsync(
"import", [ "./_content/MyCompany.MyLib/MyLib.js" ]
);
}
public async System.Threading.Tasks.Task GetDOM_ElementOffsetCoordinates(
Microsoft.AspNetCore.Components.ElementReference elementReference
)
{
return await this.getMyLib().InvokeAsync("getDOM_ElementOffsetCoordinates", [ elementReference ]);
}
private Microsoft.JSInterop.IJSObjectReference getMyLib()
{
return this.MyLib ?? throw new Exception("Expected MyLib be initialized");
}
async ValueTask IAsyncDisposable.DisposeAsync()
{
if (this.MyLibis not null)
{
await this.MyLib.DisposeAsync();
}
}
}
Код: Выделить всё
public partial class MyComponent: ComponentBase
[Microsoft.AspNetCore.Components.Inject]
protected Microsoft.JSInterop.IJSRuntime javaScriptRuntime { get; set; } = null!;
private readonly JavaScriptFunctionality javaScriptFunctionality = JavaScriptFunctionality.GetNotInitializedYetInstance();
protected override async System.Threading.Tasks.Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await this.javaScriptFunctionality.Load(this.javaScriptRuntime);
}
}
}

Microsoft.JSInterop.JSException: 'Не удалось получить динамически импортируемый модуль: https://0.0.0.1/_content/MyCompany/MyLib.js
TypeError: не удалось получить динамически импортированный модуль: https://0.0.0.1/_content/MyCompany/MyLib.js'
Как заставить его работать, когда библиотека включена в качестве локальной сборки?