繁体   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