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