【发布时间】:2021-11-26 09:00:16
【问题描述】:
我必须同时在网络和移动平台上运行我的应用程序。但是必须知道平台是移动平台还是网络平台?根据切换的平台,我要根据平台稍微修改代码。
如果有人对此有任何不同的看法,请告诉我。
【问题讨论】:
我必须同时在网络和移动平台上运行我的应用程序。但是必须知道平台是移动平台还是网络平台?根据切换的平台,我要根据平台稍微修改代码。
如果有人对此有任何不同的看法,请告诉我。
【问题讨论】:
https://material.angular.io/cdk/platform/overview Angular CDK 包含此信息
【讨论】:
.is-desktop {
display: block !important;
}
.is-mobile {
display: none !important;
}
@media screen and (max-width: 767px) {
.is-desktop {
display: none !important;
}
.is-mobile {
display: block !important;
}
}
<div class="is-mobile">Code for mobile</div>
<div class="is-desktop">Code for desktop/web</div>
如果你想用 angular typescript 来做,你可以这样做
import {BreakpointObserver} from '@angular/cdk/layout';
isSmallScreen = false;
constructor(breakpointObserver: BreakpointObserver) {
this.isSmallScreen = breakpointObserver.isMatched('(max-width: 768px)');
}
// and import this in app.module imports array
import {LayoutModule} from '@angular/cdk/layout';
imports:[LayoutModule]
【讨论】: