【问题标题】:How to include 3rd party library that uses older import approach to Angular4.x?如何在 Angular4.x 中包含使用旧导入方法的 3rd 方库?
【发布时间】:2018-01-14 22:11:08
【问题描述】:

include the library 到 angular 4.0 并在组件中使用它的正确工作流程是什么?

我的步骤:

yarn add mathjs

那么应该有一种方法可以在其中一个构建生命周期中注入 js 库,以便 angular4 组件可以使用它。 JHipster 利用 Webpack 和 Yarn。

然后我尝试添加到组件(docs):

import { mathjs } from "./mathjs";  

var math = require('mathjs');

那些不工作。我错过了什么?

更新:

似乎 mathjs 使用了建议 var math = require('mathjs') 的旧方法。也许它在某些方面类似于JQuery question...

UPDATE2

【问题讨论】:

    标签: javascript angular typescript jhipster


    【解决方案1】:

    我是这样做的,它适用于 angular9。

    首先安装npm包mathjs。

    npm install mathjs
    

    然后导入你的组件或指令。

    import { round } from 'mathjs'
    

    你可以用这个来测试。

    console.log(round(math.pi, 3) )
    

    【讨论】:

      【解决方案2】:

      这是一个很好的问题,我很高兴你提出这个问题,因为我希望我在第一次遇到这个小问题时就拥有了我将要写的内容。这是一个 typescript/javascript 和 webpack 问题,然后是角度问题。我肯定会尽快在my blog 上写一篇文章。

      您的场景:

      你跑

      npm install mathjs
      
      1. 现在您尝试使用组件中的 math.js:
      2. 找到 math.js dist js 文件 (node_modules/mathjs/dist/math.js) 并像这样引用

        import {mathjs} from "../../node_modules/mathjs/dist/math";

      3. 但你收到错误消息说"set --allowJS". 你这样做是这样的:

        Set --allowJS in config (tsconfig.json) { "compilerOptions": { "allowJs": true, ...

      4. 现在你得到:

      ../node_modules/mathjs/dist/math.js (12209,13) 中的错误:无法访问 检测到代码。

      1. 查看 math.js 源代码,您会发现它是一个老派模块,但没有根包装函数(一个函数将它们全部带入并在黑暗中绑定它们..)(稍后会详细介绍)。

      解决方案:为目标库 (@types/mathjs) 安装类型文件

      1. 首先,检查您是否可以在此处为您的模块获取 @typings 文件 https://microsoft.github.io/TypeSearch/
      2. 从 npm (https://www.npmjs.com/package/@types/mathjs) 获取 mathjs 类型文件并运行 npm install 以将类型 .d.ts 文件添加到目标库的 node_modules 目录

        npm install --save @types/mathjs

      3. 正确添加您的类型参考

        import * as mjs from "mathjs"

      像这样使用它:

      console.log("e was: " + mjs.e);
      

      我的 github 上有 math.js 库的完整解决方案 https://github.com/res63661/importOldJSDemoWithTypings/

      更多: 例如,您只需查看您自己的 Angular 项目即可。每次使用 ng new 创建新项目后运行 npm install 时,CLI 都会创建 node_modules 文件夹。深入这里并注意许多 .js 文件的 d.ts 文件。

      当打乱打字或定义自己的(.d.ts 文件)时,请务必在构建之间重新启动服务器,因为打字目前似乎没有即时更新

      进一步阅读:

      1. http://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html
      2. https://angular.io/guide/typescript-configuration#typescript-typings
      3. https://microsoft.github.io/TypeSearch/

      最后:

      如果您处于紧要关头并且这对您不起作用,我确实成功地通过将其包装在主导出类型中为不同(小得多)模块创建了自定义包装器

      export var moduleNamer = (function(){
        //module implementation
      }());
      

      然后将 .js 文件转储到我的组件本地,然后按如下方式引用它:

      //reference like this from your component:
      import {moduleNamer} from "./source";  //from source.js
      

      --丰富

      【讨论】:

        【解决方案3】:

        尝试将脚本包含在 index.html 中:

        <script src="./assets/math.js" type="text/javascript"></script>
        

        然后将其添加到您的组件文件中:

        declare const math; 
        

        然后您可以在组件中使用数学:

        ngOnInit(): void {
            console.log(math.sqrt(-4););
        }
        

        【讨论】:

        • ERROR ReferenceError: math is not defined 是从 HomeComponent 抛出的,该组件有一个需要 mathjs 的嵌套 CalculatorComponent。也许应该以某种方式将 mathjs 添加到 webpack.common.jsventor.ts...
        猜你喜欢
        • 2017-11-03
        • 2017-01-12
        • 1970-01-01
        • 1970-01-01
        • 2014-08-12
        • 2018-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多