Blazor和Vue对比学习(知识点杂锦3.02):动态组件,component和DynamicComponent
1、Vue的动态组件为component,有一个is属性,通过控制is属性来完成组件的动态切换。组件切换时,被切走的组件被销毁,状态无法保存。可以结合KeepAlive,实现状态的保存。
is="isTab1?Tab1:Tab2">
2、Blazor的动态组件为DynamicComponent,有两个属性,其中属性Type接收Type类型参数,可以通过【typeof(组件类名)】的方式获得组件的元数据。另一个属性为Parameters,必须是字典类型Dictionary
"@(isTab1?typeof(Tab1):typeof(Tab2))" Parameters="@message"> @code{ private bool isTab1 = true; //属性Parameters必须是一个字典类型 //Tab1或Tab2组件接收数据时,接收属性的类型对应value的类型,名称对应字典的key private Dictionary<string,object> message = new Dictionary<string,object>{ {"name","functionMC"}, {"age",18} }; } //Tab1组件这是Tab1组件
"请输入文字"/> @a以下为动态组件传递过来的数据
Name:@Name
Age:@Age
@code { private int a = 0; [Parameter] public string Name { get; set; } //接收键Name的Value [Parameter] public int Age { get; set; } //接收键Age的Value } //Tab2组件这是Tab2组件
以下为动态组件传递过来的数据
Age:@Age
@code { [Parameter] public int Age { get; set; } //接收键Age的Value }