简体   繁体   English

Angular 6 - Sidenav 与 ngx-translate 的内容重叠

[英]Angular 6 - Sidenav overlaps content with ngx-translate

I'm having some troubles using ngx-translate , because translated texts are shown in the selected language AFTER the main component is shown.我在使用ngx-translate遇到了一些麻烦,因为在显示主要组件,翻译文本以所选语言显示。

I have a main component like this:我有一个像这样的主要组件:

<app-header></app-header>
<mat-sidenav-container>
    <mat-sidenav [opened]="true">
        <app-menu></app-menu>
    </mat-sidenav>
    <mat-sidenav-content>
        <router-outlet></router-outlet>
    </mat-sidenav-content>
</mat-sidenav-container>
<app-footer></app-footer>

The menu component has a list with translations, like this:菜单组件有一个带有翻译的列表,如下所示:

<nav>
    <mat-list>
        <mat-list-item>{{ 'app.list.home' | translate }}</mat-list-item>
        <mat-list-item>{{ 'app.list.contact' | translate }}</mat-list-item>
    </mat-list>
</nav>

When the page is rendered, the mat-sidenav-content has a margin-left lower than mat-sidenav width.当页面呈现时, mat-sidenav-contentmargin-left低于 mat-sidenav 宽度。 I assume that it happens because the menu translations where made AFTER the margin-left was calculated.我认为这是因为在计算了margin-left之后进行的菜单翻译。 This causes the mat-sidenav overlaps the mat-sidenav-content .这会导致mat-sidenavmat-sidenav-content重叠。

I'm pretty new with angular and I don't know if there is a way to fix that, like "calling" the translation somehow before rendering the sidenav or something like that.我对 angular 很陌生,我不知道是否有办法解决这个问题,比如在渲染 sidenav 或类似的东西之前以某种方式“调用”翻译。

Best regards.此致。

我通过给 mat-nav-list 固定宽度解决了它。

<mat-nav-list style="width: 250px;">

If anyone needs an explanation:如果有人需要解释:

The margin of the component is calculated before the text is translated.在翻译文本之前计算组件的边距。 So the size of the navbar is less than its actual size.因此导航栏的大小小于其实际大小。

Solution I used:我使用的解决方案:

ngAfterViewChecked() {
  // margin are calculated before translation gives the final size for the sidenav
  window.dispatchEvent(new Event('resize'));
}

This helped: Angular Incorrect Margin-Left Applied To Sidenav Content这有帮助: 应用于 Sidenav 内容的 Angular Incorrect Margin-Left

Don't forget:不要忘记:

implements AfterViewChecked

Not sure, if its the same problem, but I had a similar case, where the size of the navigation changed after init.不确定,如果它是同样的问题,但我有一个类似的情况,在 init 之后导航的大小发生了变化。 In my case I had a collapsible sub navigation, that could increase the size to the right.就我而言,我有一个可折叠的子导航,可以向右增加大小。 A very easy solution for that was the "autosize" attribute in the <mat-sidenav-container> .一个非常简单的解决方案是<mat-sidenav-container> ”属性。

<mat-sidenav-container style="height: 100%" autosize>
...
</mat-sidenav-container>

Found the solution here: https://github.com/crisbeto/material2/blob/8187a0c6896e4e3ea2a6748d90724ac3c880cd60/src/lib/sidenav/sidenav.md#resizing-an-open-sidenav在这里找到解决方案: https : //github.com/crisbeto/material2/blob/8187a0c6896e4e3ea2a6748d90724ac3c880cd60/src/lib/sidenav/sidenav.md#resizing-an-open-sidenav

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

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