如何使用 Angular2 和 Typescript 获取设备信息

Bhr*_*jni -1 windows navigator typescript angular

我有一张表,我需要从打字稿中将这些详细信息填入我的表中。

1.Device Name
2. Device OS
3. Location
4. Browser
5. IsActive
Run Code Online (Sandbox Code Playgroud)

这些字段必须从打字稿中填写,所以任何人都可以帮我解决这个问题

Sra*_*van 8

您可以使用, ngx-device-detector

ngx-device-detector是一个Angular 2(及更多)驱动的 AOT 兼容设备检测器,有助于识别浏览器、操作系统和其他有关使用应用程序的设备的有用信息。处理基于用户代理。

安装:

要安装此库,请运行:

$ npm install ngx-device-detector --save
Run Code Online (Sandbox Code Playgroud)

用法:

Import DeviceDetectorModule in your app.module.ts

  import { NgModule } from '@angular/core';
  import { DeviceDetectorModule } from 'ngx-device-detector';
  ...
  @NgModule({
    declarations: [
      ...
      LoginComponent,
      SignupComponent
      ...
    ],
    imports: [
      CommonModule,
      FormsModule,
      DeviceDetectorModule.forRoot()
    ],
    providers:[
      AuthService
    ]
    ...
  })
Run Code Online (Sandbox Code Playgroud)

在您要使用设备服务的组件中

import { Component } from '@angular/core';
  ...
  import { DeviceDetectorService } from 'ngx-device-detector';
  ...
  @Component({
    selector: 'home',  // <home></home>
    styleUrls: [ './home.component.scss' ],
    templateUrl: './home.component.html',
    ...
  })

  export class HomeComponent {
    deviceInfo = null;
    ...
    constructor(..., private http: Http, private deviceService: DeviceDetectorService) {
      this.epicFunction();
    }
    ...
    epicFunction() {
      console.log('hello `Home` component');
      this.deviceInfo = this.deviceService.getDeviceInfo();
      console.log(this.deviceInfo);
    }
    ...
  }
Run Code Online (Sandbox Code Playgroud)

设备服务:

拥有以下属性:

  • 浏览器
  • 操作系统
  • 设备
  • 用户代理
  • os_version


归档时间:

查看次数:

6118 次

最近记录:

7 年,7 月 前