我正试图将一个列表从父组件传递给子组件,以便子组件可以使用该列表进行处理
然后,用户可以单击子组件上的按钮来处理从父组件收到的列表
错误
cannot convert from 'System.Collections.Generic.List<Test.Data.Class1>' to 'System.Collections.Generic.List<System.Type>'
类别
public class Class1
{
public string name {get; set;} = string.Empty;
public int id {get; set;}
}
父Razor页面
@if (myList.Any() != false)
{
<Component1 childList="@myList"/>
}
@code{
List<Class1> myList = new List<Class1>();
//add data into myList
...
}
子组件
<button onclick="@childFunction">Process List</button>
@code{
[Parameter]
public List<Type> childList{get; set;} = null!;
public void childFunction()
{
ProcessList(childList);
}
public static void ProcessList<T>(List<T> childList)
{
...
}
}
如果要添加通用组件,则需要在Component1.razor
中添加@typeparam Type
@typeparam Type
<button onclick="@childFunction">Process List</button>
@code {
[Parameter]
public List<Type> childList { get; set; } = null!;
public void childFunction() => ProcessList(childList);
public static void ProcessList<T>(List<T> childList)
{
}
}