在服务器上预渲染角度通用应用程序时检测设备类型

问题描述 投票:0回答:1

我正在使用ngx-device-detector库来检测设备类型(移动设备,平板电脑或台式机)。该库在客户端模式下可以正常运行,但是在服务器上预渲染有角度的通用应用程序时(在服务器上预渲染后,可以在客户端模式下完美运行),无法检测到设备类型。

  1. [如果有人为我提供了适用于该库的解决方案,我会很感激
  2. 最终,如果该库没有任何解决方案,请给我另一个解决方案

谢谢。

angular angular-universal device-detection
1个回答
1
投票

我快速看过代码,我想您可以使用可以从请求标头中检索到的用户代理字符串来调用setDeviceInfo

app.module.ts

import {Request} from 'express';
import {REQUEST} from '@nguniversal/express-engine/tokens';

constructor(@Inject(PLATFORM_ID) private platformId, 
            @Optional() @Inject(REQUEST) protected request: Request,
            private deviceService: DeviceDetectorService)
{
    if(!isPlatformBrowser(platformId))
    {
        this.deviceService.setDeviceInfo(request.headers['user-agent']);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.