简体   繁体   English

我在我的 ASP.NET Core 6 MVC 项目中包含了 Blazor 组件,但它不起作用

[英]I included Blazor component to my ASP.NET Core 6 MVC project but it doesn't work

在此处输入图像描述 It displays but doesn't work button click.它显示但不起作用按钮单击。

Index.cshtml:索引.cshtml:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.css">
</head>
<body>
<component type="typeof(ShiftsEditor)" render-mode="WebAssemblyPrerendered"/>
</body>
</html>

ShiftsEditor.razor (it is the test component): ShiftsEditor.razor(它是测试组件):

@page "/ShiftsEditor"

<h3>ShiftsEditor</h3>
<p>@prgf</p>
<button @onclick="UpdateHeading">button</button>

@code {
    public string prgf = "str";

    public void UpdateHeading() => prgf = "content updated";
}

You can check if the JS you are referencing in _Layout.cshtml is <script src="_framework/blazor.server.js"></script> or <script src="_framework/blazor.webassembly.js"></script> .您可以检查您在_Layout.cshtml中引用的 JS 是<script src="_framework/blazor.server.js"></script>还是<script src="_framework/blazor.webassembly.js"></script>

server.js needs to use render-mode="ServerPrerendered" , and webassembly.js needs to use render-mode="WebAssemblyPrerendered" . server.js需要使用render-mode="ServerPrerendered" ,而webassembly.js需要使用render-mode="WebAssemblyPrerendered"

Index.cshtml:索引.cshtml:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.css">
</head>
<body>
    <component type="typeof(ShiftsEditor)" render-mode="ServerPrerendered"/>
</body>
</html>

Add @using Microsoft.AspNetCore.Components and @using Microsoft.AspNetCore.Components.Web in razor.在razor 中添加@using Microsoft.AspNetCore.Components@using Microsoft.AspNetCore.Components.Web

ShiftsEditor.razor: ShiftsEditor.razor:

@page "/ShiftsEditor"
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Web;

<h3>ShiftsEditor</h3>
<p>@prgf</p>
<button @onclick="UpdateHeading">button</button>
@code {
    public string prgf = "str";
    public void UpdateHeading() => prgf = "content updated";
}

_Layout.cshtml: _Layout.cshtml:

<base href="~/" />
<script src="_framework/blazor.server.js"></script>

Program:程序:

builder.Services.AddServerSideBlazor();
app.UseEndpoints(endpoints =>
{
    endpoints.MapBlazorHub();
}

Test Result:测试结果:

Before click:点击前:

在此处输入图像描述

After click:点击后:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AddSingleton 在我的 ASP.NET 核心项目中不起作用 - AddSingleton doesn't work in my ASP.NET Core project 将 blazor 组件添加到现有 Asp.net 核心项目的问题 - Problem with adding a blazor component to an exsiting Asp.net core project JWT不会与Blazor一起存储在ASP.NET Core中 - JWT doesn't get stored in ASP.NET Core with Blazor 为什么在我的ASP.NET Core 1.0(MVC6)项目中看不到我的Core 1库? - Why can't my Core 1 library be seen in my ASP.NET Core 1.0 (MVC6) project? Asp.net 核心 blazor 与 Z2D50972FECD376129545507F1062089Z 核心 mvc 与 Z60C05C632A2822A0A8477C7E9916 - Asp.net core blazor vs .net core mvc with razor ASP.Net Mvc Core Project未经授权的请求不会重定向到“登录”页面 - ASP.Net Mvc Core Project unauthorized request doesn't redirect to Login page ASP.NET CORE MVC CRUD 操作。 创建表单不再起作用 - ASP.NET CORE MVC CRUD operation. Create form doesn't work anymore 编辑无法在我的 ASP.NET Core MVC 项目上正常工作 - edit not working correctly on my ASP.NET Core MVC project 为什么Test Explorer没有看到我对ASP.NET Core MVC的NUnit测试? - Why Test Explorer doesn't see my NUnit tests for ASP.NET Core MVC? 提交表单时,甜蜜警报不会发送我元素的ID(ASP.Net core mvc) - Sweet alert doesn't send the Id of my element when submiting the form (ASP.Net core mvc)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM