簡體   English   中英

路由器出口外的 Angular 子路由元素

[英]Angular child route elements outside of router-outlet

我想知道是否有一種方法可以在<router-outlet>之外實現子組件元素,就像在示例中一樣,我希望插座內的每個組件都可以在父組件上顯示自己的按鈕。

<div class="header">
  <h1>Page Title</h1>
  <div class="action-buttons">
    <!-- child component buttons -->
  </div>
</div>

<div id="wrapper">
  <router-outlet></router-outlet>
</div>

我不確定這是否是最好的方法,但我正在做的是每次路線更改時使用 Renderer2 將按鈕從子級移動到父級。

this.router.navigate(["/"]).then(() => {
   this.moveButtons();
});
 private moveButtons() : void {
    const parent = document.querySelector('.float-buttons');
    const buttonsParent = document.getElementById('childButtons');

    parent.childNodes.forEach(child => {
      this.renderer.removeChild(parent,child);
    });

    if(!buttonsParent) return;
    const children = buttonsParent.childNodes;

    children.forEach(child => {
      this.renderer.appendChild(parent, child);
    });
  }

暫無
暫無

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

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