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

How to fetch the device information using Angular2 and Typescript

我有一个 table,我需要用 typescript 将这些详细信息填写到我的 table 中。

1.Device Name
2. Device OS
3. Location
4. Browser
5. IsActive

这些字段必须用打字稿填写,所以谁能帮我解决这个问题

可以使用,ngx-device-detector

ngx-device-detector is An Angular 2 (and beyond) powered AOT compatible device detector that helps to identify browser, os and other useful information regarding the device using the app. The processing is based on user-agent.

安装:

要安装这个库,运行:

$ npm install ngx-device-detector --save

用法:

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
    ]
    ...
  })

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

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);
    }
    ...
  }

设备服务:

拥有以下属性:

  • 浏览器
  • os
  • 设备
  • 用户代理
  • os_version