簡體   English   中英

如何在雙向綁定 asp.net 內核中的 blazor C# 的 3 個嵌套組件之間進行通信(從父代到其孫代,反之亦然)

[英]How to communicate between 3 nested components of blazor C# in two-way binding asp.net core (from parent to its grandchild vice versa)

我需要在后面的代碼中在 blazor 中的 3 個組件之間進行通信。

  • 父組件有一個(提交按鈕)按鈕單擊事件,在后面的代碼中,我需要通過傳遞一些參數來調用 child-1 組件並等待其作為bool 值作為 return響應。
  • child-1 組件通過使用確認按鈕調用對話框彈出窗口與 child-2 進行通信。
  • 當用戶單擊 child-2 組件中的確認按鈕時,我需要將響應發送回 child-1,因為status = true 否則為 false。
  • 然后 child-1 將 child-2 的響應返回給 parent 作為status=true

注意: Child-1 組件充當中間組件,它沒有任何 UI 元素

這是概念,我不知道如何在 parent 后面的代碼中調用 child-1 組件

示例: [在父組件中]

<button @onclick="callChildOne">Submit<button/>

void callChildOne(){
// i need to call child-1 here and pass the parameter status
// i need help in writing this code
}

參考這張圖片以獲得更好的理解

我建立了這個案例,並提出了 blazor 的答案。 參考下面的例子

Child2.razor

@code{//some code}

兒童1.razor

//No UI, only methods in code behind
    @code{
       public void Show()
       {
        // TODO actual implementation for calling child 2 component
       }
    }

父.razor

<button @onclick="callChildOne">Submit<button/>

@*Add a @ref to the component *@
<Child1 @ref="childObj"></Child1 >

@code {
    //Hold the reference to the component
    private Child1 childObj;

    // Call the public methods of the component
    private void callChildOne() => childObj.Show();    
}

這就是我在沒有任何服務的情況下與 3 個組件映射關系的方式。 在這里從 Blazor的博客中閱讀了這個示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM