前言

CDK Layout 主要是用于处理 Breakpoints,它底层是依靠 window.matchMedia 来实现的。

Material Design 2 & 3 Breakpoints

Material Design 有对 Breakpoints 的规范,Angular CDK 依照的是 Material Design 2。

相关源码在 breakpoints.ts

没什么特别的,它就只是一个 Definition,不同 Viewport Size / Device 对应不同的 Media Query。

如果我们不喜欢 Material Design 2 的规范,我们自己也可以 Define 一个 Material Design 3 的 Breakpoints

export const M3Breakpoints = {
Compact: '(max-width: 599.98px)',
Medium: '(min-width: 600px) and (max-width: 839.98px)',
Expanded: '(min-width: 840px) and (max-width: 1199.98px)',
Large: '(min-width: 1200px) and (max-width: 1599.98px)',
Extralarge: '(min-width: 1600px)', PhoneInPortrait: '(max-width: 599.98px)',
TabletInPortrait: '(min-width: 600px) and (max-width: 839.98px)', PhoneInLandscape: '(min-width: 840px) and (max-width: 1199.98px)',
TabletInLandscape: '(min-width: 840px) and (max-width: 1199.98px)', DesktopSmall: '(min-width: 840px) and (max-width: 1199.98px)',
DesktopMedium: '(min-width: 1200px) and (max-width: 1599.98px)',
DesktopLarge: '(min-width: 1600px)',
};

MediaMatcher

MediaMatcher 是一个 Root Level Injector Service Provider。

它是 Angular CDK 对 window.mediaMatch 的封装。

源码在 media-matcher.ts

使用方式

export class AppComponent {
constructor() { // 1. inject MediaMatcher
const mediaMatcher = inject(MediaMatcher); // 2. match media query
const mediaQueryList = mediaMatcher.matchMedia('(max-width: 599.98px)'); // 3. 使用 MediaQueryList
const isMatched = mediaQueryList.matches;
mediaQueryList.addEventListener('change', () => {});
}
}

和 window.matchMedia 用法一模一样。

BreakpointObserver

BreakpointObserver 是一个 Root Level Injector Service Provider。

isMatched 方法

BreakpointObserver.isMatched 方法可以判断当前的 viewport 是否满足特定的 media query。

export class AppComponent {
constructor() {
// 1. inject BreakpointObserver
const breakpointObserver = inject(BreakpointObserver); // 2. 判断当前 viewport 是否满足 media query
const matched1 = breakpointObserver.isMatched('(max-width: 599.98px)'); // 3. 判断当前 viewport 是否满足 Material Design 2 XSmall media query
const matched2 = breakpointObserver.isMatched(Breakpoints.XSmall); // 4. 判断当前 viewport 是否满足 Material Design 3 Compact media query
const matched3 = breakpointObserver.isMatched(M3Breakpoints.Compact);
}
}

isMatched 还支持 multiple media query,只要其中一个 matched 就算 matched。

const matched3 = breakpointObserver.isMatched(
[
M3Breakpoints.Compact,
M3Breakpoints.Medium
]
);

传入一个 array 就可以了。

或者用 string join by 逗号也行

const matched5 = breakpointObserver.isMatched(`${M3Breakpoints.Compact}, ${M3Breakpoints.Medium}`); 

逛源码

源码在 breakpoints-observer.ts

调用 _regsiterQuery 方法然后拿 mql.matches,我们继续看 mql 是什么

_regsiterQuery 方法

简而言之

const matched1 = breakpointObserver.isMatched('(max-width: 599.98px)');
const matched2 = window.matchMedia('(max-width: 599.98px)').matches;

这 2 句是等价的。

observe 方法

constructor() {
// 1. inject BreakpointObserver
const breakpointObserver = inject(BreakpointObserver); // 2. 监听 media query
breakpointObserver.observe(
[
M3Breakpoints.Compact,
M3Breakpoints.Medium
]
).subscribe(({ breakpoints, matches }) => {
console.log('breakpoints', breakpoints);
console.log('matches', matches);
});
}

效果

observe 和 isMatched 用法大同小异,只有 2 个区别:

  1. 次数不同

    isMatched 只判断当前 viewport 一次,而 observe 会一直监听 viewport (resize),然后每一次重新判断。

    因此 isMatched 返回的是 boolean,而 observe 返回的是可监听的 RxJS Observable。

  2. 返回的 result 不同

    当有你有 multiple media query 时,

    isMatched 只能告诉你整体是 matched or not matched,你无法知道具体是哪一个 media query matched。

    observe 除了告诉你整体是 matched or not matched,它还会返回一个对象,里面的 key 是 media query,value 是表示这个 media query 有没有 matched。

observe 源码我们就不逛,它底层实现方式是 window.matchMedia + addEventListener(''change),上一 part 逛源码时已经有看到一点了。

另外,subscribe observe 返回的 Observable 会立马触发一次,获得当下 viewport match media query result,这点和 window.matchMedia + addEventListener(''change) 不同哦。

总结

虽然它们只是对 window.matchMedia 的小封装,但还是鼓励大家用,尽量不要直接依赖 window 是顺风水的。

目录

上一篇 Angular Material 18+ 高级教程 – CDK Portal

下一篇 Angular Material 18+ 高级教程 – CDK Scrolling

想查看目录,请移步 Angular 18+ 高级教程 – 目录

喜欢请点推荐,若发现教程内容以新版脱节请评论通知我。happy coding

Angular Material 18+ 高级教程 – CDK Layout の Breakpoints的更多相关文章

  1. [转]VS Code 扩展 Angular 6 Snippets - TypeScript, Html, Angular Material, ngRx, RxJS & Flex Layout

    本文转自:https://marketplace.visualstudio.com/items?itemName=Mikael.Angular-BeastCode VSCode Angular Typ ...

  2. Angular Material 教程之布局篇

    Angular Material 教程之布局篇 (一) : 布局简介https://segmentfault.com/a/1190000007215707 Angular Material 教程之布局 ...

  3. Angular Material TreeTable Component 使用教程

    一. 安装 npm i ng-material-treetable --save npm i @angular/material @angular/cdk @angular/animations -- ...

  4. Angular Material design设计

    官网: https://material.io/design/ https://meterial.io/components 优秀的Meterial design站点: http://material ...

  5. Material使用11 核心模块和共享模块、 如何使用@angular/material

    1 创建项目 1.1 版本说明 1.2 创建模块 1.2.1 核心模块 该模块只加载一次,主要存放一些核心的组件及服务 ng g m core 1.2.1.1 创建一些核心组件 页眉组件:header ...

  6. Angular 2 to Angular 4 with Angular Material UI Components

    Download Source - 955.2 KB Content Part 1: Angular2 Setup in Visual Studio 2017, Basic CRUD applicat ...

  7. Siki_Unity_2-9_C#高级教程(未完)

    Unity 2-9 C#高级教程 任务1:字符串和正则表达式任务1-1&1-2:字符串类string System.String类(string为别名) 注:string创建的字符串是不可变的 ...

  8. Angular Material Starter App

      介绍 Material Design反映了Google基于Android 5.0 Lollipop操作系统的原生应用UI开发理念,而AngularJS还发起了一个Angular Material ...

  9. 关于 Angular引用Material出现node_modules/@angular/material/button-toggle/typings/button-toggle.d.ts(154,104): error TS2315: Type 'ElementRef' is not generic.问题

    百度了好久 ,,,最后谷歌出来了.. 该错误可能来自于您将@ angular / material设置为6.0.0, 但所有其他Angular包都是5.x.您应该始终确保Material主要版本与An ...

  10. Angular Material & Hello World

    前言 Angular Material(下称Material)的组件样式至少是可以满足一般的个人开发需求(我真是毫无设计天赋),也是Angular官方推荐的组件.我们通过用这个UI库来快速实现自己的i ...

随机推荐

  1. .NET周刊【7月第2期 2024-07-14】

    国内文章 开源GTKSystem.Windows.Forms框架让C# winform支持跨平台运行 https://www.cnblogs.com/easywebfactory/p/18289178 ...

  2. 靶机: hard_socnet2

    靶机: hard_socnet2 准备 靶机:https://download.vulnhub.com/boredhackerblog/hard_socnet2.ova MD5 验证: 9d6bed1 ...

  3. hadoop hive hbase flume sqoop基本操作

    top 里的id为cpu空闲度 如果wa为99.8就是负担太重.得停掉一些任务 cat /proc/cpuinfo 查看cpu信息 cat /proc/meminfo 查看内存信息 hadoop基础操 ...

  4. Jmeter二次开发函数 - 将指定时间转换为时间戳

    1.达到效果:在jmeter的函数助手增加一个"timeStamp"函数,调用"timeStamp"函数可以将用户传入的时间转换为时间戳. 2.eclipse项 ...

  5. postfix&dovecot搭建邮件服务器

    本篇参考 https://blog.51cto.com/5001660/2377785和小翔博客https://www.liuyixiang.com/post/113927.html. 邮件发送和接受 ...

  6. SQL连续查询问题拓展—记上海拼多多非技术岗面试真题

    真巧,昨天刚写了关于数据库连续问题的解决方案,没想到今天下午两点就有朋友在上海拼多多面试非技术岗位中就遇到了相似的问题.下面是原题: 一个最大连续支付失败的次数 有一张支付流水表pay;字段如下 id ...

  7. 代码随想录Day2

    209.长度最小的子数组 给定一个含有 n 个正整数的数组和一个正整数 target . 找出该数组中满足其总和大于等于 target 的长度最小的 子数组 $ [nums_l, nums_{l+1} ...

  8. 【OracleDB】 08 子查询

    什么是子查询? 子查询是一种常用计算机语言SELECT-SQL语言中嵌套查询下层的程序模块. 当一个查询是另一个查询的条件时,称之为子查询. Oracle的子查询语法公式: SELECT select ...

  9. Ubuntu 18.04在已经安装Docker CE后如何安装NVIDIA-Docker,以使docker容器内可以使用宿主机显卡

    注意: 本文是在电脑上已经安装好docker环境的前提下进行的. docker环境如何安装参照前文. 本文内容节选自:  https://baimafujinji.blog.csdn.net/arti ...

  10. 强化学习中经验池的替代设计——A3C算法

    读论文<Asynchronous methods for deep reinforcement learning>有感 ---------------------------------- ...