首页 > 解决方案 > 从 npm 包中导入 Angular 模块

问题描述

我想在应用程序中使用detect-mobile包(https://github.com/hgoebl/mobile-detect.js/tree/v1.4.4Angular

但它不能以通常的方式导入。我已经尝试了以下各项:

import { MobileDetect } from 'mobile-detect';
import MobileDetect from 'mobile-detect';
import {}  from 'mobile-detect';

移动检测d.ts文件具有以下结构:

在此处输入图像描述

我做错了什么,应该如何导入?另外,我将非常感谢您解释此类软件包与其他软件包的不同之处。

Stackblitz:https ://stackblitz.com/edit/angular-3qptjt

标签: javascriptangularnpmpackage

解决方案


用于导出的语法 - export = MobileDetect- 表示该包是使用 CommonJS 样式导出编写的。请参阅此票以获取更多说明。

因此,导入它的方法是:

import * as MobileDetect from 'mobile-detect'

另外,我相信如果你使用esModuleInteropTypeScript 编译器标志,你可以写:

import MobileDetect from 'mobile-detect'

推荐阅读