gpt4 book ai didi

Angular Material : BreakpointObserver not available from load?

转载 作者:太空狗 更新时间:2023-10-29 17:58:33 38 4
gpt4 key购买 nike

在结合使用 Angular BreakpointObserverAngular Material 时,我尝试使用 Material Design 规范的断点(它们在 Breakpoints 中定义>,例如 Breakpoints.LandscapePortraitBreakpoints.HandsetPortrait)。

breakpointObserver 工作,除了当我刚加载页面时。 breakpointObserver 仅在观察到变化之后 被触发,但考虑到它是一个新的负载,目前还没有变化。这意味着初始视口(viewport)尚未根据断点进行评估。我试过在 OnInit 中使用单个 BreakpointObserver.isMatched,但这似乎没有任何效果。

我将 BreakpointObserverBreakpointsMediaMatcher 包含在一个共享服务中,我订阅了所有需要“监听”的组件到断点。

我的问题是:如何确保在第一次视口(viewport)更改之前评估断点(如果用户不调整窗口大小/更改设备方向,这可能根本不会发生)?

这是我的 shared.service.ts 的代码:

import {Component, OnDestroy, OnInit, Injectable,Input,Output,EventEmitter} from '@angular/core';
import {BreakpointObserver, Breakpoints, MediaMatcher} from '@angular/cdk/layout';

@Injectable()
export class SharedService implements OnDestroy, OnInit {
public isCollapsed = false;
public isOpen = false;
public isMobileView = false;
public isTabletView = false;
private breakpointObserver: BreakpointObserver;
@Output() mediaChange = new EventEmitter();
constructor(breakpointObserver: BreakpointObserver) {
this.breakpointObserver = breakpointObserver;
// check if the viewport matches Material Design-spec handset displays
this.breakpointObserver.observe([
Breakpoints.HandsetPortrait
]).subscribe(result => {
if (result.matches) {
this.isMobileView = true;
this.isTabletView = false;
this.isOpen = false;
this.isCollapsed = false;
}
else {
this.isMobileView = false;
this.isOpen = true;
this.isCollapsed = false;
}
this.mediaChanged();
});
// check if the viewport matches Material Design-spec tablet displays
this.breakpointObserver.observe([
Breakpoints.TabletPortrait
]).subscribe(result => {
if (result.matches) {
this.isTabletView = true;
this.isMobileView = false;
this.isOpen = true;
this.isCollapsed = true;
}
else {
if(!this.isMobileView){
this.isOpen = true;
}
this.isTabletView = false;
this.isCollapsed = false;
}
this.mediaChanged();
});
}
mediaChanged() {
this.mediaChange.emit({
"isMobileView" : this.isMobileView,
"isTabletView" : this.isTabletView,
"isCollapsed" : this.isCollapsed,
"isOpen" : this.isOpen
});
}
ngOnInit() {
// MY ATTEMPT
// Running the same checks as the observer, but this time on init(?)
// does not seem to take any effect
if(this.breakpointObserver.isMatched([
Breakpoints.HandsetPortrait
])){
this.isMobileView = true;
this.isTabletView = false;
this.isOpen = false;
this.isCollapsed = false;
}
if(this.breakpointObserver.isMatched([
Breakpoints.TabletPortrait
])){
this.isTabletView = true;
this.isMobileView = false;
this.isOpen = true;
this.isCollapsed = true;
}
this.mediaChanged();
}
}

最佳答案

将局部变量设置为 false 并根据订阅中的值更改它。

import { Component, OnInit } from '@angular/core';
import { BreakpointObserver, Breakpoints, BreakpointState } from '@angular/cdk/layout';
import { ActivatedRoute } from '@angular/router';

@Component({
selector: 'app-main-menu',
templateUrl: './main-menu.component.html',
styleUrls: ['./main-menu.component.css']
})
export class MainMenuComponent implements OnInit {
isHandset = false;

constructor(private breakpointObserver: BreakpointObserver, private route: ActivatedRoute) {}

ngOnInit() {
this.breakpointObserver.observe(Breakpoints.Handset)
.subscribe((state: BreakpointState) => {
if (state.matches) {
this.isHandset = true;
} else {
this.isHandset = false;
}
});
}

}

模板代码:

<mat-sidenav-container class="sidenav-container">
<mat-sidenav #drawer class="sidenav" fixedInViewport
[attr.role]="isHandset ? 'dialog' : 'navigation'"
[mode]="isHandset ? 'over' : 'side'"
[opened]="isHandset === false">
<mat-toolbar>Menu</mat-toolbar>
<mat-nav-list>
<a mat-list-item href="#">Link 1</a>
<a mat-list-item href="#">Link 2</a>
<a mat-list-item href="#">Link 3</a>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
<mat-toolbar color="primary">
<button
type="button"
aria-label="Toggle sidenav"
mat-icon-button
(click)="drawer.toggle()"
*ngIf="isHandset">
<mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
</button>
<span>timplaw-dotcom</span>
</mat-toolbar>
<!-- Add Content Here -->
<router-outlet></router-outlet>
</mat-sidenav-content>
</mat-sidenav-container>

引用:https://alligator.io/angular/breakpoints-angular-cdk/

关于 Angular Material : BreakpointObserver not available from load?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921259/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com