如何在Razor页面中运行的Razor组件中触发事件?
我的初创公司:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
});
}我的Razor页面调用组件:
@page
@model DocketDetail.OrderModel
@{
Layout = null;
}
@using RelationalObjectLayerCore;
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/lib/jquery/dist/jquery.js"></script>
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="_framework/blazor.server.js"></script>
<script src="~/js/site.js"></script>
<title>Order</title>
</head>
<body>
<component type="typeof(Component.Filedby)" render-mode="ServerPrerendered" />
</body>所有内容都显示正常。
我的组件:
@using Microsoft.AspNetCore.Components
@code {
private void SearchPerson()
{
string x = "TEST";
}
}
<button @onclick="SearchPerson">Search</button>显然,这是从我的实际代码中删减的……但是我不知道如何让"SearchPerson“在Razor组件中触发。
发布于 2020-01-31 02:33:38
带着这张小纸条:
在结束
</body>标记内为blazor.server.js脚本添加一个<script>标记:
超文本标记语言
移动了我的脚本标签,它现在可以工作了。
感谢大家的建议。
发布于 2020-01-31 01:03:50
我想我明白你想做什么了。我不确定这是否会像你想的那样工作。组件将使用Blazor呈现,但附加到按钮的代码将不起作用。将按钮的@code{}放在加载的页面上。或者,如果希望每次加载页面时都运行该代码,则可以执行OnInitialized()重载。
此外,请确保Blazor在浏览器中正确渲染。使用F12并确保其显示为connected。
https://docs.microsoft.com/en-us/aspnet/core/blazor/lifecycle?view=aspnetcore-3.1
https://stackoverflow.com/questions/59990682
复制相似问题