You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi guys:
I'm a newer in blazor,razor,blazor hybrid and i have some confused in load a script from an external js file.
My expected effect is make my library like Mudblazor that can import js or css file into some blazor (hybrid) project by using tag <script src="_content/{PACKAGE ID}/{PATH}/{PAGE, VIEW, OR COMPONENT}.{EXTENSION}.js"></script>. So i created a solution BlazorHybridProject means it's a test project and a solution JsLibraryProject means it's a RCL project and BlazorHybridProject will reference it.
After projects created, my index.html looks like:
@page "/counter"
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<prole="status">Current count: @currentCount</p>
<buttonclass="btn btn-primary"@onclick="IncrementCount">Click me</button>
@code{privateintcurrentCount=0;
// for import and invoke js function : prompt which is located at wwwroot directory in BlazorApp1.UI project
[Inject]
publicExampleJsInteropExampleJsInterop{get; set; }// for import and invoke js function: prompt1 which is located at wwwroot directory in BlazorApp1.JsLibrary project
[Inject]
publicExampleJsInteropFromLibraryJsInteropFromLibrary{get; set; }privatevoidIncrementCount()
{currentCount++;
ExampleJsInterop?.Prompt($"The count in BlazorApp1.UI.Counter is :{currentCount}");
JsInteropFromLibrary?.Prompt($"The count in BlazorApp1.UI.Counter is :{currentCount}");
}}
ExampleJsInteropFromLibrary.cs like:
usingMicrosoft.JSInterop;namespaceBlazorApp1.JsLibrary{// This class provides an example of how JavaScript functionality can be wrapped// in a .NET class for easy consumption. The associated JavaScript module is// loaded on demand when first needed.//// This class can be registered as scoped DI service and then injected into Blazor// components for use.publicclassExampleJsInteropFromLibrary:IAsyncDisposable{privatereadonlyLazy<Task<IJSObjectReference>>moduleTask;publicExampleJsInteropFromLibrary(IJSRuntimejsRuntime){moduleTask=new(()=>jsRuntime.InvokeAsync<IJSObjectReference>("import","./_content/BlazorApp1.JsLibrary/exampleJsInterop.min.js").AsTask());}publicasyncValueTask<string>Prompt(stringmessage){varmodule=awaitmoduleTask.Value;returnawaitmodule.InvokeAsync<string>("showPrompt1",message);}publicasyncValueTaskDisposeAsync(){if(moduleTask.IsValueCreated){varmodule=awaitmoduleTask.Value;awaitmodule.DisposeAsync();}}}}
When i build and run my project and click the Click me button in counter page, the embedded js file in BlazorApp1.UI project is works well but i get an error GET https://localhost:7090/_content/BlazorApp1.JsLibrary/exampleJsInterop.min.js net::ERR_ABORTED 404 in chrome console and i can't find my BlazorApp1.JsLibrary/exampleJsInterop.min.js in chrome dev-tools's Sources tab. could anyone tell me where i did it wrong or anyone can tell me how can i pack my razor class library like the Mudblazor do?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys:
I'm a newer in blazor,razor,blazor hybrid and i have some confused in load a script from an external js file.
My expected effect is make my library like Mudblazor that can import js or css file into some blazor (hybrid) project by using tag
<script src="_content/{PACKAGE ID}/{PATH}/{PAGE, VIEW, OR COMPONENT}.{EXTENSION}.js"></script>
. So i created a solutionBlazorHybridProject
means it's a test project and a solutionJsLibraryProject
means it's a RCL project andBlazorHybridProject
will reference it.After projects created, my
index.html
looks like:And my
Counter.razor
looks like:ExampleJsInteropFromLibrary.cs
like:When i build and run my project and click the
Click me
button in counter page, the embedded js file inBlazorApp1.UI
project is works well but i get an errorGET https://localhost:7090/_content/BlazorApp1.JsLibrary/exampleJsInterop.min.js net::ERR_ABORTED 404
in chrome console and i can't find my BlazorApp1.JsLibrary/exampleJsInterop.min.js in chrome dev-tools's Sources tab. could anyone tell me where i did it wrong or anyone can tell me how can i pack my razor class library like theMudblazor
do?Attaches:
Chrome console:

Chrome sources:

My project zip file:
Blazor Js Isolation.zip
Beta Was this translation helpful? Give feedback.
All reactions