如何将router-outlet组件定位到window的顶层?

How to position a router-outlet component as the top layer of the window?

我正在 Angular 应用中构建布局,如下所示:

<app-navbar></app-navbar>
<div class="router-wrapper">
    <router-outlet></router-outlet>
</div>

我的问题是我在 <router-outlet> 中加载的组件中有一个侧边栏,这个侧边栏应该放在屏幕上所有内容的顶部;但无论我用 z-index 或 position absolute 做什么,它总是在顶部显示 "behind" 导航栏;这真的可以做到吗?

这是可以做到的。如果您使用的元素样式正确,则应将其放置在页面顶部。如果您没有为 nav-bar 使用任何 z-index 值或绝对位置,那么您可能只需要将 position: absolute 和 z-index:10 用于sidemenu组件使其领先其他元素。如果您为 nav-bar 使用任何 z-index 值,那么您需要为边栏设置比 nav-bar 更高的 z-index 值。

Example

如果包装器成为其子项的 z-indexes 的根,则包装器不能位于静态以外的位置。

作为替代方案,我建议使用网格布局并将顶部栏作为由路线控制的布局的一部分(如果你对侧边栏这样做,为什么不对所有)。

Further explanation of how z-index and its context works