c# - 如何在子组件中访问父组件类型?
问题描述
在子组件中,如何访问对父组件的引用?如果它嵌套在特定类型的父级中,我需要在子级中执行特定操作。
<MyParentComponent>
<MyChildComponent></MyChildComponent>
</MyParentComponent>
解决方案
有很多方法可以做到这一点。
如果您只需要父级的一个属性/方法,您可以为该属性/方法传递一个参数,如您在docs中所见。
如果你想要整个父母,你可以使用 aCascadingValue
和 pass this
。
例如
MyParentComponent.razor
<CascadingValue Value="this">
@ChildContent
</CascadingValue>
在 中MyChildComponent
,您可以通过CascadingParameter
.
[CascadingParameter]
public MyParentComponent MyParentComponent { get; set; }
如果子组件不是父组件的直接子组件并且您不想跨嵌套组件传递多个参数,这将是获取父引用的好方法。
推荐阅读
- sql - (Postgre)SQL - 按最近日期加入 <= 另一个日期
- django - nginx 添加新站点导致 ERR_TOO_MANY_REDIRECTS
- python - 如何使用python将ed25519算法实现到jwt中?
- java - Mockito:InOrder.verify 不禁止对模拟进行中间调用
- c# - 无效错误:参数数量与存储过程的值数量不匹配
- visual-studio-code - 使用 4 个制表符空间保存时自动格式化
- xamarin.forms - 尽管安装了更多最新的 SDK,但无法更改目标框架
- python - 调用函数而不导入同一目录中的其他文件函数
- php - 标头未全局发送
- ray - 如何评估在 rllib (Ray) 中自定义环境中训练的演员?