2

I'm tring to run a javascript function called loadJsOrCssFile which takes 2 string parameters using the code below :

await JSRuntime.InvokeAsync<string>("loadJsOrCssFile", $"/brands/{_brand.BrandId}/brand{_brand.BrandId}_{_brand.CSSFileVersionNo}.css", "css");

But I get an error (from Chrome dev tools console) :

blazor.webassembly.js:1 WASM: System.ArgumentNullException: Value cannot be null.
h.printErr @ blazor.webassembly.js:1
12:37:36.127 blazor.webassembly.js:1 WASM: Parameter name: jsRuntime
h.printErr @ blazor.webassembly.js:1
12:37:36.127 blazor.webassembly.js:1 WASM:   at Microsoft.JSInterop.JSRuntimeExtensions.InvokeAsync[TValue] (Microsoft.JSInterop.IJSRuntime jsRuntime, System.String identifier, System.Object[] args) <0x281f210 + 0x00014> in <3eedf0ca90ca4e72bf6870618ca98c7c>:0 

2 Answers 2

3

Sorry, this doesn't matter now I have worked it out. I was doing this at the top of the class (this is a normal class and not within a component) :

[Inject]
IJSRuntime JSRuntime { get; set; }

It works if I get the JSRuntime through DI within the constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Paul, thanks for updating us with a solution. It might be helpful to also include the correct way to accomplish it and not just what you did wrong - to help newbies to blazor in the future.
2

For other users that came here from google, you have to inject it in the traditional way

    public class MyClass
    {
        private readonly IJSRuntime _jSRuntime;

        public MyClass (IJSRuntime jSRuntime)
        {
            _jSRuntime = jSRuntime;
        }
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.