简体   繁体   English

Angular cdk 断点和普遍问题

[英]Angular cdk breakpoints and universal issue

When rendering angular universal the breakpoint hits come too late.当渲染角度通用时,断点命中来得太晚了。 I'm using mat-sidenav which expects the drawer to be set opened or not based on a breakpoint.我正在使用 mat-sidenav,它希望根据断点将抽屉设置为打开或不打开。 It seems that the breakpoint information isn't there when rendering ssr.渲染ssr的时候好像没有断点信息。

For example, this goes in the constructor:例如,这在构造函数中:

this.isHandset$ = this.breakpointObserver.observe(Breakpoints.Handset)
    .pipe(
        map(result => result.matches)
    );

And in the view:在视图中:

<mat-sidenav-container class="sidenav-container" fullscreen>
<mat-sidenav class="sidenav" 
    [opened]="(isHandset$ | async) === false">  <!-- close if handset -->
... etc

after the app hydrates (after the full angular project loads) the drawer opens / closes as it should based on the viewport size, but initially not.在应用程序水合之后(在完整的角度项目加载之后)抽屉根据视口大小打开/关闭,但最初不是。

How to make breakpoints also work with universal?如何使断点也适用于通用?

I'm currently using angular 9 rc, but same problem in 8.我目前正在使用 angular 9 rc,但在 8 中存在同样的问题。

You can define a default value that is emitted first before any breakpoints using startWith .您可以使用startWith定义在任何断点之前首先发出的默认值。

this.isHandset$ = this.breakpointObserver.observe(Breakpoints.Handset)
    .pipe(
        map(result => result.matches),
        startWith(false)
    );

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

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