2 回答

TA贡献1815条经验 获得超6个赞
试试这个:服务-
import { Injectable } from '@angular/core';
const SMALL_WIDTH_BREAKPOINT = 720;
@Injectable({
providedIn: 'root'
})
export class ScreenSizeService {
private mediaMatcher: MediaQueryList =
matchMedia(`(max-width: ${SMALL_WIDTH_BREAKPOINT}px)`);
constructor(
) {}
isScreenSmall(): boolean {
return this.mediaMatcher.matches;
}
}
在组件中使用:
isScreenSmall: boolean;
@HostListener('window:resize') onresize() {
this.isScreenSmall= this.service.isScreenSmall();
}
constructor(private service: ScreenSizeService
) { }

TA贡献1828条经验 获得超6个赞
提到的 isMobile 库依赖于用户代理字符串,所以它基本上不需要另一个页面加载。
但是,通常只有移动设备支持“TouchEvent”。您可以使用它来检查它是否可能是移动设备,而无需依赖用户代理
function isTouch() {
try {
document.createEvent('TouchEvent');
return true;
}
catch (e) {
return false;
}
}
添加回答
举报