如何在typescript中创建window.mediaQuery匹配项
我有很多列的表,我想根据浏览器宽度隐藏,所以我尝试使用window.matchMedia((最大宽度:700)”).matches,window.media((最大宽度:500)”).matches等。。在typescript中,但我无法获取媒体侦听器。请找到密码如何在typescript中创建window.mediaQuery匹配项,typescript,media-queries,Typescript,Media Queries,我有很多列的表,我想根据浏览器宽度隐藏,所以我尝试使用window.matchMedia((最大宽度:700)”).matches,window.media((最大宽度:500)”).matches等。。在typescript中,但我无法获取媒体侦听器。请找到密码 for(let i : number =0 ; i< this.col.length; i++){ let media, media = window.matchMedia(col[i].minWidth); this.mcol
for(let i : number =0 ; i< this.col.length; i++){
let media,
media = window.matchMedia(col[i].minWidth);
this.mcol.push(col[i])
this.mediaQueryUpdate(media)
media.addEventLister(mediaQueryUpdate)
}
public medaiQueryUpdate(e:any){
if(window.matchMedia(e.target.media).match){ // did not get target e , error thrown as e does not found
for(let c: number =0 ; c< this.mcol.length; c++){
if(window.matchMedia(e.target.media) == this.mcol[c].minWidth ){
// did nto get target from e
mcol[c].hide= true;
}
}
}
for(设i:number=0;i
如何在typescript中创建window.mediaQuery匹配项
最快的解决方案,只需使用断言:
if ((window.matchMedia(e.target.media) as any).match) {
更多
- 断言:
- 向lib.d.ts添加类型:
isSmallMobileDevice: MediaQueryList = window.matchMedia("(max-width: 599px)")
您为什么不使用css来执行此操作?我们不知道最大宽度/最小宽度动态变化。您仍然可以在css中将“显示”属性设置为“无”。。