简体   繁体   English

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

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

I was wondering if there is a way to implement a child component element out of the <router-outlet> , like in the example, I wish each component inside of the outlet could show its own buttons on the parent component.我想知道是否有一种方法可以在<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>

I'm not sure if it's the best way but what I'm doing is moving the buttons from the child to the parent using Renderer2 every time the route changes.我不确定这是否是最好的方法,但我正在做的是每次路线更改时使用 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