javascript - 如何在另一个 JS 应用程序中导入 blazor 自定义元素?

标签 javascript blazor blazor-server-side custom-element

我有一个 blazor 服务器应用程序,注册的自定义元素如下代码所示:

builder.Services.AddServerSideBlazor(options =>
{
    options.RootComponents.RegisterAsCustomElement<Counter>("my-blazor-counter");
});

我想在另一个 node.js 应用程序中导入此 blazor 自定义元素,以将其转换为 lit 元素(Web 组件)。 我在我的 node.js 应用程序中添加了以下脚本

<script src="https://localhost:7075/_framework/blazor.server.js"></script>
<script src="https://localhost:7075/_content/Microsoft.AspNetCore.Components.CustomElements/BlazorCustomElements.js"></script>

但是在初始化 Blazor 时它仍然使用节点应用程序端口并且在初始化时失败。 我不确定我在这里遗漏了什么,或者是否有任何其他方法可以做到这一点。

最佳答案

下面描述了我如何解决与您的问题类似的问题:尝试注册自定义元素,客户端不呈现组件并且任何地方都没有错误消息。

我关注了the instructions但是客户端没有发生任何事情。在使用 Firefox 检查 websocket 的流量后,我遇到了从客户端到服务器的以下消息(为了便于阅读而稍作编辑):

ùÀµEndInvokeJSFromDotNetÂÚÙ[
    2,
    false,
    "Could not find 'registerBlazorCustomElement' ('registerBlazorCustomElement' was undefined).
    findFunction/<@https://localhost:5001/_framework/blazor.server.js:1:497
    findFunction@https://localhost:5001/_framework/blazor.server.js:1:465
    E@https://localhost:5001/_framework/blazor.server.js:1:2606
    attachWebRendererInterop/<@https://localhost:5001/_framework/blazor.server.js:1:33097
    attachWebRendererInterop@https://localhost:5001/_framework/blazor.server.js:1:33145
    beginInvokeJSFromDotNet/s<@https://localhost:5001/_framework/blazor.server.js:1:3501
    beginInvokeJSFromDotNet@https://localhost:5001/_framework/blazor.server.js:1:3475
    _invokeClientMethod/<@https://localhost:5001/_framework/blazor.server.js:1:71894
    _invokeClientMethod@https://localhost:5001/_framework/blazor.server.js:1:71880
    _processIncomingData@https://localhost:5001/_framework/blazor.server.js:1:69922
    kt/this.connection.onreceive@https://localhost:5001/_framework/blazor.server.js:1:64322
    connect/</o.onmessage@https://localhost:5001/_framework/blazor.server.js:1:48638
    EventHandlerNonNull*connect/<@https://localhost:5001/_framework/blazor.server.js:1:48489
    connect@https://localhost:5001/_framework/blazor.server.js:1:48005
    _startTransport@https://localhost:5001/_framework/blazor.server.js:1:57626
    _createTransport@https://localhost:5001/_framework/blazor.server.js:1:56195
    _startInternal@https://localhost:5001/_framework/blazor.server.js:1:54044
    async*start@https://localhost:5001/_framework/blazor.server.js:1:51309
    _startInternal@https://localhost:5001/_framework/blazor.server.js:1:66198
    _startWithStateTransitions@https://localhost:5001/_framework/blazor.server.js:1:65598
    start@https://localhost:5001/_framework/blazor.server.js:1:65262
    Gn@https://localhost:5001/_framework/blazor.server.js:1:129904
    Yn@https://localhost:5001/_framework/blazor.server.js:1:127771
    async*@https://localhost:5001/_framework/blazor.server.js:1:131523
    @https://localhost:5001/_framework/blazor.server.js:1:131529
    "
]

在我的例子中,我没有添加 <script src="/_content/Microsoft.AspNetCore.Components.CustomElements/BlazorCustomElements.js"></script>到 html。

关于javascript - 如何在另一个 JS 应用程序中导入 blazor 自定义元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70428988/

相关文章:

.net - 如何使用区域在 razor 组件库中使用 blazor 服务器端?

asp.net - 服务器端 Blazor 与 MVC

javascript - ExpressJS 子类在回调中缺少继承的属性和属性

javascript - Canvas图像绘制顺序

c# - 如何在服务器端应用程序中获取本地数据源?

c# - 如何在 Blazor Server 中实现自定义授权

c# - .net core 3 - b2c roles auth - 声明注入(inject)不起作用

javascript - jquery 自动完成不会触发

javascript - 如何在客户端获取服务器端 html 复选框的值?

asp.net-core - 如何从浏览器中清除 Blazor WebAssembly DLL?