Blazor WebAssembly Dotnet 5 Javascript Interop 无法找到 JS 函数

huangapple go评论72阅读模式
英文:

Blazor WebAssembly Dotnet 5 Javascript Interop can't find JS function

问题

I am getting this error while trying to run JS in Blazor WA dotnet 6:
> Microsoft.JSInterop.JSException: 在‘bweInterop’中找不到‘bweInterop.getWindowSize’(‘bweInterop’未定义)。
>Error: 在‘bweInterop’中找不到‘bweInterop.getWindowSize’(‘bweInterop’未定义)。

My index.razor is:

    @page "/"
    @using BwaJsInterop.Model
    @inject IJSRuntime Js
    <PageTitle>Home</PageTitle>
    @if (_windowSize.Width != null)
    {
        <h2>
            窗口大小: @_windowSize.Width x @_windowSize.Height
        </h2>
    }
    <button @onclick="GetWindowSize">获取窗口大小</button>
    @code {
        private WindowSize _windowSize = new();
        private async Task GetWindowSize()
        {
            _windowSize = await Js.InvokeAsync<WindowSize>(
                "bweInterop.getWindowSize");
        }
    }

I placed the external javascript in the wwwroot/index.js:

<body>
    <div id="app">加载中...</div>

    <div id="blazor-error-ui">
        发生了未处理的错误。
        <a href="" class="reload">重新加载</a>
        <a class="dismiss">🗙</a>
    </div>
    <script src="_framework/blazor.webassembly.js"></script>
    <script src="js/bweInterop.js"></script> 
</body>

Javascript is located here:

Blazor WebAssembly Dotnet 5 Javascript Interop 无法找到 JS 函数

And finally the javascript file itself:

let bweInterop = {};
bweInterop.getWindowSize = function () {
    return {
        width: window.innerWidth,
        height: window.innerHeight
    };
}

In the browser console, I ran bweInterop.getWindowSize() function and it works, so the problem is not the javascript.

Blazor WebAssembly Dotnet 5 Javascript Interop 无法找到 JS 函数

Am I missing anything?

英文:

I am getting this error while trying to run JS in Blazor WA dotnet 6:
> Microsoft.JSInterop.JSException: Could not find 'bweInterop.getWindowSize' ('bweInterop' was undefined).
>Error: Could not find 'bweInterop.getWindowSize' ('bweInterop' was undefined).

My index.razor is:

    @page "/"
    @using BwaJsInterop.Model
    @inject IJSRuntime Js
    <PageTitle>Home</PageTitle>
    @if (_windowSize.Width != null)
    {
        <h2>
            Window Size: @_windowSize.Width x @_windowSize.Height
        </h2>
    }
    <button @onclick="GetWindowSize">Get Window Size</button>
    @code {
        private WindowSize _windowSize = new();
        private async Task GetWindowSize()
        {
            _windowSize = await Js.InvokeAsync<WindowSize>(
                "bweInterop.getWindowSize");
        }
    }

I placed the external javascript in the wwwroot/index.js:

<body>
    <div id="app">Loading...</div>

    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>
    <script src="_framework/blazor.webassembly.js"></script>
    <script src="js/bweInterop.js"></script> 
</body>

Javascript is located here:

Blazor WebAssembly Dotnet 5 Javascript Interop 无法找到 JS 函数

And finally the javascript file itself:

let bweInterop = {};
bweInterop.getWindowSize = function () {
    return {
        width: window.innerWidth,
        height: window.innerHeight
    };
}

In the browser console, I ran bweInterop.getWindowSize() function and it works, so the problem is not the javascript.

Blazor WebAssembly Dotnet 5 Javascript Interop 无法找到 JS 函数

Am I missing anything?

答案1

得分: 2

你可能遇到了Blazor的JavaScript模块隔离问题(请参见此处)。尝试在你的Blazor组件内明确加载JS文件,即:

@page "/"
@using BwaJsInterop.Model
@inject IJSRuntime Js
<PageTitle>Home</PageTitle>
@if (_windowSize.Width != null)
{
    <h2>
        Window Size: @_windowSize.Width x @_windowSize.Height
    </h2>
}
<button @onclick="GetWindowSize">Get Window Size</button>
@code {
    private WindowSize _windowSize = new();
    private IJSObjectReference? module;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            module = await Js.InvokeAsync<IJSObjectReference>("import", "./js/bweInterop.js");
        }
    }

    private async Task GetWindowSize()
    {
        _windowSize = await module.InvokeAsync<WindowSize>(
            "bweInterop.getWindowSize");
    }
}
英文:

Maybe you're running into issues with Blazor's JavaScript module isolation (see here). Try explicitly loading your JS file inside your Blazor component, i. e.:

@page &quot;/&quot;
@using BwaJsInterop.Model
@inject IJSRuntime Js
&lt;PageTitle&gt;Home&lt;/PageTitle&gt;
@if (_windowSize.Width != null)
{
    &lt;h2&gt;
        Window Size: @_windowSize.Width x @_windowSize.Height
    &lt;/h2&gt;
}
&lt;button @onclick=&quot;GetWindowSize&quot;&gt;Get Window Size&lt;/button&gt;
@code {
    private WindowSize _windowSize = new();
    private IJSObjectReference? module;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            module = await Js.InvokeAsync&lt;IJSObjectReference&gt;(&quot;import&quot;, &quot;./js/bweInterop.js&quot;);
        }
    }

    private async Task GetWindowSize()
    {
        _windowSize = await module.InvokeAsync&lt;WindowSize&gt;(
            &quot;bweInterop.getWindowSize&quot;);
    }
}

huangapple
  • 本文由 发表于 2023年5月25日 08:59:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定