简体   繁体   English

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

[英]Detecting the device type when an angular universal app pre-rendered on the server

I'm using ngx-device-detector library for detecting the device type (mobile, tablet or desktop).我正在使用ngx-device-detector库来检测设备类型(移动设备、平板电脑或台式机)。 This library working perfectly in client mode but can't detect the device type when an angular universal application pre-rendered on the server(after prerendering on the server, working perfectly on client mode).该库在客户端模式下完美运行,但在服务器上预渲染 angular 通用应用程序时无法检测设备类型(在服务器上预渲染后,在客户端模式下完美运行)。

  1. I appreciate if someone gives me a solution that works for this library如果有人给我一个适用于该库的解决方案,我将不胜感激
  2. Eventually, if there isn't any solution for this library, give me another solution最终,如果这个库没有任何解决方案,请给我另一个解决方案

Thanks.谢谢。

I had a quick look at the code and I think you can call setDeviceInfo with a user agent string that you can retrieve from the request headers我快速查看了代码,我认为您可以使用可以从请求标头中检索的用户代理字符串调用setDeviceInfo

app.module.ts 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']);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM