简体   繁体   English

如何获得Angular的 <router-outlet> 使用CSS Grid

[英]How to get Angular's <router-outlet> to work with CSS Grid

I'm trying to create a layout with Angular 7 and CSS Grid. 我正在尝试使用Angular 7和CSS Grid创建布局。 The problem I have is that the router-outlet only takes up one column in the second row. 我的问题是router-outlet仅占用第二行的一列。

I have created a StackBlitz here . 我在这里创建了StackBlitz

As you can see in app.component.css I want router-outlet (and any of its content) to take up grid-column: 2 / 5 , as of now it takes up 2 / 3. 如您在app.component.css中看到的,我希望router-outlet (及其任何内容)占据grid-column: 2 / 5 app.component.css ,截至目前,它占据了2/3。

Is what I'm trying to achieve possible? 我要实现的目标有可能吗?

<router-outlet> has no size and does not have any content in it, the actual router content is the next element to it. <router-outlet>没有大小,并且其中没有任何内容,实际的路由器内容是它的下一个元素。 Remove the styles from <router-outlet> and only apply it to the next element to that and you should be fine. <router-outlet>删除样式,仅将其应用于下一个元素,就可以了。

Also, your app.component tries to apply styles to components that are not included in your component. 另外,您的app.component尝试将样式应用于未包含在组件中的组件。 You have to specify this explicitly by using ViewEncapsulation.None in the component definition. 您必须通过在组件定义中使用ViewEncapsulation.None来明确指定。 See here: https://stackblitz.com/edit/angular-lmpnkl 看到这里: https : //stackblitz.com/edit/angular-lmpnkl

I think the problem is that you are trying to apply the style directly to <router-outlet> . 我认为问题在于您尝试将样式直接应用于<router-outlet> Instead, create a "main div" element and put the router outlet inside of it. 而是创建一个“ main div”元素,并将路由器出口放入其中。

<app-header></app-header>
<app-navbar></app-navbar>
<div main>
  <router-outlet></router-outlet>
</div>
<app-footer></app-footer>

I've updated your stackblitz to demonstrate. 我已更新您的堆叠闪电战以进行演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM