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

Kam*_*dos 3 device-detection angular-universal angular

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

  1. 如果有人给我一个适用于这个库的解决方案,我很感激
  2. 最后,如果这个库没有任何解决方案,请给我另一个解决方案

谢谢。

Dav*_*vid 5

我快速浏览了代码,我认为您可以setDeviceInfo使用可以从请求标头中检索的用户代理字符串进行调用

应用程序模块.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']);
    }
}
Run Code Online (Sandbox Code Playgroud)