Skip to main content

Definition

Assembly: CloudNimble.BlazorEssentials.dll Namespace: CloudNimble.BlazorEssentials Inheritance: System.Object

Syntax

CloudNimble.BlazorEssentials.JsModule

Summary

A wrapper that makes it easier to dynamically import JavaScript modules in Blazor. Can be used as the foundation to build strongly-typed .NET wrappers around JavaScript libraries.

Remarks

I built this because trying to remember the same pattern for importing JS modules in Blazor was driving me nuts.

Constructors

.ctor

Creates a new instance of the JsModule class.

Syntax

public JsModule(Microsoft.JSInterop.IJSRuntime jsRuntime, string modulePath = "")

Parameters

NameTypeDescription
jsRuntimeMicrosoft.JSInterop.IJSRuntimeThe IJSRuntime instance that was likely injected by the ViewModel / Page / Control this module is
being used in.
modulePathstringThe full path to the JS file to wrap. Should usually be in the format ”../content//.js”.

Remarks

If you don’t provide a modulePath, the constructor will attempt to infer it from the calling assembly, in the format ”../_content//.js”.

.ctor

Creates a new instance of the JsModule class.

Syntax

public JsModule(Microsoft.JSInterop.IJSRuntime jsRuntime, string packageName, string modulePath)

Parameters

NameTypeDescription
jsRuntimeMicrosoft.JSInterop.IJSRuntimeThe IJSRuntime instance that was likely injected by the ViewModel / Page / Control this module is
being used in.
packageNamestring-
modulePathstringThe path to the file, usually from the ‘wwwroot’ folder in the base of the project.

Remarks

The SDK-style project system actually does a really good job of knowing when the PackageId is different from the AssemblyName, so this constructor may not be necessary.

.ctor Inherited

Inherited from object

Syntax

public Object()

Properties

Instance

Returns a Lazy1](https://learn.microsoft.com/dotnet/api/system.lazy-1) reference to the [Task1 of importing the module through IJSRuntime.

Syntax

public System.Lazy<System.Threading.Tasks.Task<Microsoft.JSInterop.IJSObjectReference>> Instance { get; private set; }

Property Value

Type: System.Lazy<System.Threading.Tasks.Task<Microsoft.JSInterop.IJSObjectReference>>

Remarks

We’re using Lazy1](https://learn.microsoft.com/dotnet/api/system.lazy-1) here to ensure that the module is imported exactly once and only when needed. [Lazy1 manages the instance for us

Methods

DisposeAsync

Disposes of

Syntax

public System.Threading.Tasks.ValueTask DisposeAsync()

Returns

Type: System.Threading.Tasks.ValueTask

Equals Inherited Virtual

Inherited from object

Syntax

public virtual bool Equals(object obj)

Parameters

NameTypeDescription
objobject?-

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

public static bool Equals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

GetHashCode Inherited Virtual

Inherited from object

Syntax

public virtual int GetHashCode()

Returns

Type: int

GetType Inherited

Inherited from object

Syntax

public System.Type GetType()

Returns

Type: System.Type

InvokeAsync

Syntax

public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args = null)

Parameters

NameTypeDescription
identifierstring-
argsobject?[]?-

Returns

Type: System.Threading.Tasks.ValueTask<TValue>

Type Parameters

  • TValue -

InvokeAsync

Syntax

public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, System.Threading.CancellationToken cancellationToken, object[] args = null)

Parameters

NameTypeDescription
identifierstring-
cancellationTokenSystem.Threading.CancellationToken-
argsobject?[]?-

Returns

Type: System.Threading.Tasks.ValueTask<TValue>

Type Parameters

  • TValue -

InvokeAsync

Syntax

public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, System.TimeSpan timeout, object[] args = null)

Parameters

NameTypeDescription
identifierstring-
timeoutSystem.TimeSpan-
argsobject?[]?-

Returns

Type: System.Threading.Tasks.ValueTask<TValue>

Type Parameters

  • TValue -

MemberwiseClone Inherited

Inherited from object

Syntax

protected internal object MemberwiseClone()

Returns

Type: object

ReferenceEquals Inherited

Inherited from object

Syntax

public static bool ReferenceEquals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

ToString Inherited Virtual

Inherited from object

Syntax

public virtual string ToString()

Returns

Type: string?
  • Microsoft.JSInterop.IJSObjectReference
  • System.IAsyncDisposable