【发布时间】:2020-07-08 22:23:58
【问题描述】:
我想知道,如何使用来自 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>
Index.razor 页面
@page "/{typeName}"
@using BlazorApp1.Components
<MyCustomComponent T="@TypeName" Label="Custom component label" />
@code
{
[Parameter]
public string TypeName {get; set;}
}
所以我尝试通过字符串属性设置类型的行我得到编译器错误。 (我猜想内部的 typeof() 函数将确定组件类的类型。) 如果我使用固定编码类型,它只会编译。 :
e.g.: <MyCustomComponent T="long" Label="Custom component label" />
我想要做的是从 URL 中提取类型参数。有什么想法可以完成这项工作吗?
【问题讨论】:
标签: asp.net-core blazor