【问题标题】:is there any ways to detect the application is currently in which platform like mobile or web?有什么方法可以检测应用程序当前在哪个平台上,例如移动设备或网络?
【发布时间】:2021-11-26 09:00:16
【问题描述】:

我必须同时在网络和移动平台上运行我的应用程序。但是必须知道平台是移动平台还是网络平台?根据切换的平台,我要根据平台稍微修改代码。

如果有人对此有任何不同的看法,请告诉我。

【问题讨论】:

    标签: angular angular6


    【解决方案1】:

    【讨论】:

    • 感谢您的回复。这不符合我的要求。但我有“navigator.userAgent”方法来检测设备。
    【解决方案2】:
    .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]
    

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2011-11-24
      相关资源
      最近更新 更多