我想知道,如何使用来自URL的@typeparam
调用组件
正如C# Blazor: How to use @typeparam in Code behind? (with workaround)中已经讨论的那样,我构建了类似于Roger Wolf在其解决方案中所做的组件。
Raozor.cs 使用Microsoft.AspNetCore.Components;
namespace BlazorApp1.Components
{
public partial class MyCustomComponent<T> : ComponentBase
{
[Parameter]
public string Label { get; set; }
}
}
剃刀部分:
@namespace BlazorApp1.Components
@typeparam T
<label>@($"{Label}. Provided type is {typeof(T).Name.ToUpper()}")</label>
索引剃刀页面
@page "/{typeName}"
@using BlazorApp1.Components
<MyCustomComponent T="@TypeName" Label="Custom component label" />
@code
{
[Parameter]
public string TypeName {get; set;}
}
因此,我尝试通过字符串属性设置Type的Line会出现编译器错误。 (我猜想内部的typeof()函数将确定组件类的类型。) 如果使用固定编码类型,它将仅编译。 :
e.g.: <MyCustomComponent T="long" Label="Custom component label" />
我要做的是从URL中提取type参数。有任何想法可以完成这项工作吗?
答案 0 :(得分:0)
@typeparam
的类型是在编译时得出的,并且您的组件中应该有T
类型的参数:
namespace BlazorApp1.Components
{
public partial class MyCustomComponent<T> : ComponentBase
{
[Parameter]
public T MyParameter { get; set; }
}
}
它用于制作通用组件,不能将其用作示例。