【问题标题】:Import Angular module from npm package从 npm 包中导入 Angular 模块
【发布时间】:2020-01-30 05:00:52
【问题描述】:

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

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

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

手机检测d.ts文件结构如下:

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

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

【问题讨论】:

    标签: javascript angular npm package


    【解决方案1】:

    用于导出的语法 - export = MobileDetect - 表示该包是使用 CommonJS 样式导出编写的。更多解释请见this ticket。

    因此导入的方法是:

    import * as MobileDetect from 'mobile-detect'
    

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

    import MobileDetect from 'mobile-detect'
    

    【讨论】:

    • 但是与茉莉花理论有什么区别?它包含:declare var jsminTheories: jasmineTheories.jasmineTheoriesStatic; export default jsminTheories; 并且可以像这样导入:import theoretically from 'jasmine-theories'; 如果您在答案中添加一些解释,请感谢。
    • 现在我看到 export default jsminTheories 是 ES6 模块。但是您能否将解释添加到答案中
    猜你喜欢
    • 1970-01-01
    • 2021-01-17
    • 2019-12-22
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    相关资源
    最近更新 更多