【问题标题】:Import 3rd party library导入第三方库
【发布时间】:2017-01-30 14:14:10
【问题描述】:

我需要在 angular2 项目中导入第 3 方库。

这是我所做的:

ng new myproject
npm install --save createjs-easeljs
npm install @types/easeljs

现在是我陷入困境的时刻。如何导入和使用这个库?有ShapeStage之类的对象

import { Shape, Stage } from '../../../node_modules/createjs-easeljs/lib/easeljs-0.8.2.min.js';

这根本不起作用。

我的文件夹结构:

dynam194:src timo$ tree -L 2
.
├── app
│   ├── app.component.css
│   ├── app.component.html
│   ├── app.component.spec.ts
│   ├── app.component.ts
│   ├── app.module.ts
│   └── canvas
├── assets
├── environments
│   ├── environment.prod.ts
│   └── environment.ts
├── favicon.ico
├── index.html
├── main.ts
├── polyfills.ts
├── styles.css
├── test.ts
├── tsconfig.json
└── typings
    └── easeljs.d.ts

tsconfig.json

"paths": {
  "easeljs": ["../node_modules/createjs-easeljs/lib/easeljs-0.8.2.min.js"]
},
"sourceMap": true,
"target": "es5",
"typeRoots": [
  "../node_modules/@types",
  "typings",
]

【问题讨论】:

    标签: javascript angular typescript angular-cli


    【解决方案1】:

    您必须将路径(和 baseUrl)添加到您的 tsconfig.json

    {
      "compilerOptions": {
        ...
        "baseUrl: ".",
        "paths": {
          "easeljs": ["../node_modules/createjs-easeljs/lib/easeljs-0.8.2.min.js"]
        },
      }
    }
    

    easeljs 的路径相对于您的tsconfig.json

    之后,您可以使用以下方法导入此库:

    import * as createjs from 'easeljs';
    

    并在您的项目中使用它,例如:

    let stage: createjs.Stage = new createjs.Stage('myCanvas');
    let shape: createjs.Shape = new createjs.Shape();
    

    因为@types 定义文件都有自己定义模块的方式,您可能会遇到与这种导入方式不兼容的方式。但据我所知,他们希望将其作为标准。

    您应该创建一个本地类型文件夹并创建一个 easeljs.d.ts 文件,其中包含:

    /// <reference path="../../node_modules/@types/easeljs/index.d.ts" />
    
    declare module "createjs" {
        export = createjs;
    }
    

    确保引用路径指向正确的目录,我不知道你的项目结构:)

    然后将本地类型文件夹添加到您的tsconfig.jsontypeRoots 属性:

    {
      "compilerOptions": {
        ...
        "typeRoots": [
          "../node_modules/@types",
          "typings/local"
        ]
       }
    }
    

    更新

    显然,这不适用于这个库。很遗憾。好在 angular-cli 有一个预加载脚本的选项:

    编辑您的 angular-cli.json 以添加库:

    {
      "apps": [
        {
          ...
          "scripts": [
            "../node_modules/createjs-easeljs/lib/easeljs-0.8.2.min.js"
          ],
        }
      ]
    }
    

    不要在文件顶部使用导入,因此请删除 import * as createjs from 'easeljs'。并且不需要本地类型文件夹。还要删除tsconfig.json 中的paths

    【讨论】:

    • 我收到以下错误:Module build failed: Error: /myproject/src/app/canvas/canvas.component.ts (2,27): File '/myproject/node_modules/@types/easeljs/index.d.ts' is not a module.) 其中 2,27 是您刚刚编写的导入。
    • 这是@types 的一个令人讨厌的特征。我已经更新了答案
    • 嗯,好像没用。同样的错误。我的打字路径是./typings,这就是我所做的不同。我也确实将参考路径更改为...@types/easeljs/index.d.ts...。也许是 createjs/easeljs 的命名问题?
    • 我刚刚在我自己的项目中安装了easeljs,它就像我描述的那样工作。尽管 ref 路径错误,但您是对的。我已经相应地更新了我的答案。您确定您在tsconfig.json 中输入了正确的路径吗?在您的情况下,它看起来像是指向 .d.ts 文件,但它应该指向 .min.js
    • 而您的 tsconfig.jsonsrc 文件夹中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多