最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use ua-parser-js in angular 4 - Stack Overflow

programmeradmin4浏览0评论

I want os name, os version, browser name, browser version and device from the user agent. How can I use user agent parser js in my application in angular 4. I tried [email protected] but it doent return me the os version and device. Please help me

I want os name, os version, browser name, browser version and device from the user agent. How can I use user agent parser js in my application in angular 4. I tried [email protected] but it doent return me the os version and device. Please help me

Share Improve this question asked Feb 28, 2019 at 12:15 DonDon 371 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

To add it to package.json:

npm install ua-parser-js

To use it in your ponents or services you need an import for it to work:

import { UAParser } from 'ua-parser-js'; 

let parser = new UAParser();
console.log(parser.getResult());

I found the missing import in this other SO answer. Then you can use the documentation to get whatever information you're looking for.

The UAParser.js documentation is your best ally.

You can get OS name and version, browser name and other information using UAParser.js like that:

var parser = new UAParser();
// by default it takes ua string from current browser's window.navigator.userAgent
console.log(parser.getResult());
var result = parser.getResult();

console.log(result.browser);        // {name: "Chromium", version: "15.0.874.106"}
console.log(result.device);         // {model: undefined, type: undefined, vendor: undefined}
console.log(result.os);             // {name: "Ubuntu", version: "11.10"}
console.log(result.os.version);     // "11.10"
console.log(result.engine.name);    // "WebKit"
console.log(result.cpu.architecture);   // "amd64"
发布评论

评论列表(0)

  1. 暂无评论