Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Looking into new .NET8 Blazor Web App. How to add a .js to a .razor page?

$
0
0

It should be very simple, but I really can't find a way to add a .js to project.I have created a .razor page with a simple code like this:

@page "/text"@rendermode InteractiveAuto@inject IJSRuntime JsRuntime @implements IAsyncDisposable <h2>Test  @text</h2><button @onclick="LoadText">Load</button>@code{    string text = "";    private IJSObjectReference module;    protected override async Task OnAfterRenderAsync(bool firstRender)    {        if (firstRender)        {            module = await JsRuntime.InvokeAsync<IJSObjectReference>("import", "./js/index.js");        }    }    private async Task LoadText()    {        text = await module.InvokeAsync<string>("logToConsole");    }}

and a .js file with function I put in created in the wwwroot directory named js:

export function logToConsole() {    return "My text"}

Instead of appending text, It throws an error:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]      Unhandled exception rendering component: Failed to resolve module specifier '.js/index.js'      TypeError: Failed to resolve module specifier '.js/index.js'          ...

Tried to change path, tried all kind of rendering modes, but it didn't help.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>