Vs2019 16.3.1 dotnetcore 3.0css
dotnet new webapp -o projectname
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddServerSideBlazor();//启用服务端blazor支持 }
Configure 方法html
app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapBlazorHub();// js,singalr });
使用Vs打开项目,默认页面和目录暂时不动, 新增文件夹 RazorComponentsvue
在项目根目录下,右键菜单添加-新建项-Razor组件,命名_Imports.razor
,用于导入razor组件所需命名空间(做用相似mvc中的_ViewImports.cshtml)。
此文件对同级或子级文件夹中的*.razor生效, 将内容替换为
@using Microsoft.AspNetCore.Components @using Microsoft.AspNetCore.Components.Web;
git
在RazorComponents 文件夹上 右键菜单添加-新建项-Razor组件,命名CounterButton.razor
。github
说明:web
CounterButton.razor
.razor文件本质为一个继承ComponentBase,名为CounterButton的c#类(全名为项目名称.文件夹.CounterButton)。
打开CounterButton.razor 文件能够看到,@code指令(预览6以前的@functions)将文件分为两部分,上部为html标签,下部即为CounterButton类的实现部分。
CounterButton.razorc#
<h3>Component</h3> @code {//可脑补为 public class CounterButton:ComponentBase{ //c#代码,属性、方法。。 }
[Parameter]// 用于传递参数 public int Count { get; set; } void Click() { Count++; }
处理Html标记, CounterButton.razor 内容以下
<button @onclick="Click" > Count:@Count </button> @code { [Parameter] public int Count { get; set; } void Click() { Count++; } }
此时组件代码已完成,接下来转到Pages目录下,处理.cshtml浏览器
打开Pages/Index.cshtml,在你想要显示组件的地方插入代码
@(await Html.RenderComponentAsync<RazorComponents.CounterButton>(RenderMode.Server))
mvc
@(await Html.RenderComponentAsync<CounterButton>(RenderMode.Server))
<script src="_framework/blazor.server.js"></script>
在以前的组件代码中有这样一行
[Parameter]// 用于传递参数 public int Count { get; set; }
能够用来设置初始化参数,若是在另外一个.razor 文件中,咱们能够这样设置 Count的初始值
<CounterButton Count="2" />
可是,使用Html.RenderComponentAsync 时, RenderMode 为Server或ServerPrerendered 不支持参数。RenderMode.Static 仅输出静态Html(没法与服务端交互)。app
在目前阶段,咱们可使用一个无参数的razor组件过渡一下。
//根据业务,将须要组合的razor组件放在一个组件内,能够方便的处理参数及组件间的关系 <CounterButton Count="3"/>// 在_Imports.razor 中加入@using projectname.RazorComponents 或使用全名<projectname.RazorComponents.CounterButton Count="3"/>
@(await Html.RenderComponentAsync<RazorPanel>(RenderMode.Server))
前面说过,组件是继承 ComponentBase的,所以能够用一个c#代码文件实现组件,如下以Icon为例。
public class IconBase: Microsoft.AspNetCore.Components.ComponentBase { [Parameter] public string IconName { get; set; } = "icon-user"; [Parameter] public string IconClass { get; set; } = "icon"; }
@inherits IconBase
@inherits IconBase <svg class="@IconClass" aria-hidden="true"> <use href="#@IconName"></use> </svg> <style scoped> .icon { height: 1em; /* 经过设置 font-size 来改变图标大小 */ width: 1em; /* 图标和文字相邻时,垂直对齐 */ vertical-align: -0.15em; /* 经过设置 color 来改变 SVG 的颜色/fill */ fill: currentColor; /* path 和 stroke 溢出 viewBox 部分在 IE 下会显示 normalize.css 中也包含这行 */ overflow: hidden; } </style>
razor组件上可使用ChildContent 属性嵌套其余组件,好比须要在CounterButton中加入一个Icon.
在CounterButton.razor 中增长一个属性
[Parameter] public RenderFragment ChildContent { get; set; }
<button @onclick="Click"> @ChildContent Count:@Count </button>
打开RazorPanel.razor,修改 CounterButton 标记
<CounterButton Count="3"> <ChildContent> <Icon IconName="icon-user" /> </ChildContent> </CounterButton>
或 省略 <ChildContent>
<CounterButton Count="3" > <Icon IconName="icon-user" /> </CounterButton>
在想引用的子组件上定义 @ref ="组件引用名",在当前组件上定义同名字段捕获引用。
好比 在 RazorPanel.razor 中,给CounterButton 增长属性
<CounterButton Count="3" @ref="button" > <Icon IconName="icon-user" /> @button.Count </CounterButton>
@code
CounterButton button;//自动捕获 @ref="button" 的CounterButton 实例
RenderFragment 委托,ComponentBase.BuildRenderTree 方法
dotnet new -i Microsoft.AspNetCore.Blazor.Templates