我有两个组成部分。
第一部分包括型号 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/