我一直在试验棱角material ,发现自己质疑在一个简单的媒体查询中使用内置BreakpointObserver API的好处?

或者,与其说它更有益,不如说它真的取决于你想要实现什么?

推荐答案

它通常用于使手机和桌面的页面不同.例如,您可以在JavaScript文件中设置断点并使其对更改做出react .

例如,如果您try 在移动设备上的输入字段中搜索,您可以导航到整个页面,只需让用户在桌面上搜索即可

  navigateToSearchPageIfMobile = () => {
    combineLatest([this.isMobile$, this.searchActive$]).pipe(
      takeUntil(this.unsubscribe$),
      filter(([isMobile, active]) => isMobile && active))
      .subscribe(() => {
        this._router.navigate(["./", this._languageService.lang$.value, paths.MARKETOVERVIEW, paths.SEARCH], {queryParams: {search: this.searchText$.value}})
      })
  }

或者仅仅是数据驱动的设计,*ngif

或者,您可以创建自己的断点服务:

export class BreakpointService {

    constructor() {
        this.onLoad()
        this.onResize()
    }

    private readonly breakpoints = {
        xs: 0,
        sm: 576,
        md: 768,
        lg: 992,
        xl: 1200
    }

    private currentWidthSub$ = new BehaviorSubject<number>(null)
    public currentWidth$ = this.currentWidthSub$.asObservable()

    isMobile$ = this.currentWidth$.pipe(map(x => x < this.breakpoints.md), distinctUntilChanged())

    currentBreakpoint$ = this.currentWidthSub$.pipe(
        filter(width => width != null),
        map(width => {
            return Object.keys(this.breakpoints).map((key) => {
                return {
                    key: key,
                    value: this.breakpoints[key] - 1
                }
            }).reverse().find(x => x.value < width).key
        }),
        distinctUntilChanged()
    )

    private onResize = () => {
        fromEvent(window, 'resize').subscribe(resize => {
            this.currentWidthSub$.next(resize.currentTarget["innerWidth"])
        })
    }

    private onLoad = () => {
        fromEvent(window, 'load').subscribe(load => {
            this.currentWidthSub$.next(load.currentTarget["innerWidth"])
        })

    }  
}

Typescript相关问答推荐

为什么在将条件返回类型的字符串赋给数字时没有类型错误?

脉轮ui打字脚本强制执行图标按钮的咏叹调标签-如何关闭

node—redis:如何在redis timeSeries上查询argmin?

编剧错误:正在等待Expect(Locator).toBeVisible()

诡异的文字类型--S为物语的关键

在HighChats列时间序列图中,十字准线未按预期工作

如何通过TypeScrip中的函数防止映射类型中未能通过复杂推理的any值

声明文件中的类型继承

如何将父属性类型判断传播到子属性

从子类集合实例化类,同时保持对Typescript中静态成员的访问

确保对象列表在编译时在类型脚本中具有唯一键

专用路由组件不能从挂钩推断类型

如何将类似数组的对象(字符串::匹配结果)转换为类型脚本中的数组?

为什么发送空字段?

有没有办法传递泛型类型数组,以便推断每个元素的泛型类型?

TypeScrip:如何缩小具有联合类型属性的对象

是否可以在类型脚本中定义条件返回类型,而不在函数体中使用类型转换?

TypeScrip:使用Cypress测试包含插槽的Vue3组件

在TypeScript中,如何通过一系列过滤器缩小类型?

必须从注入上下文调用Angular ResolveFn-inject()