gpt4 book ai didi

blazor - 如何从blazor的父组件调用子组件方法?

转载 作者:行者123 更新时间:2023-12-03 14:31:00 29 4
gpt4 key购买 nike

我有两个组成部分。
第一部分包括型号 list
第二部分包含模态形式
我要在第一个组件中单击模型
在第二个组件中,打开模态并编辑模型
如何从父组件调用子组件中的show函数

<ChildComponent />
<button onClick="@ShowModal">show modal</button>

@code{
ChildComponent child;

void ShowModal(){
child.Show();
}
}

我习惯了@using但
此代码有错误:

the type or namespace name ChildComponent coud not be found

最佳答案

首先,您需要获取子组件的引用:

<ChildComponent @ref="child" />
然后,您可以像在代码中一样使用此引用来调用子组件方法。
<button onClick="@ShowModal">show modal</button>

@code{
ChildComponent child;

void ShowModal(){
child.Show();
}
}
需要在页面或_Imports.razor中使用来添加组件的 namespace 。如果您的组件位于子文件夹Components/ChildComponent.razor中,则其命名空间为{YourAppNameSpace}。
@using MyBlazorApp.Components
read the code

关于blazor - 如何从blazor的父组件调用子组件方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60041619/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com