简体   繁体   English

如何在angular2组件中导入npm包?

[英]How to import a npm package in an angular2 component?

I'm trying to learn the ropes of ng2 and the depedency injection system is killing me. 我正在尝试学习ng2的精髓,而去污剂注入系统正在杀死我。

I'm using the ng quickstart from: https://github.com/angular/quickstart/blob/master/README.md 我正在从以下位置使用ng quickstart: https : //github.com/angular/quickstart/blob/master/README.md

I'm trying to import this package into the app: https://www.npmjs.com/package/arpad . 我正在尝试将此软件包导入应用程序: https : //www.npmjs.com/package/arpad I installed the package via npm update, my package.json dependencies look like this: 我通过npm update安装了软件包,我的package.json依赖项如下所示:

"dependencies": {
  "angular2": "2.0.0-beta.9",
  "systemjs": "0.19.24",
  "es6-promise": "^3.0.2",
  "es6-shim": "^0.35.0",
  "reflect-metadata": "0.1.2",
  "rxjs": "5.0.0-beta.2",
  "zone.js": "0.5.15",
  "arpad":"^0.1.2" <----- the package i'm trying to import
}

This is how the package exports its code: 包是这样导出其代码的:

module.exports = ELO;

I have a component importing the module like this: 我有一个像这样导入模块的组件:

import {ELO} from 'node_modules/arpad/index.js';

This is how systemJS is configured in the application's index.html: 这是在应用程序的index.html中配置systemJS的方式:

  <script>
  System.config({
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      }
    },
    map:{'arpad':'node_modules/arpad'} <---- here 
  });
  System.import('node_modules/arpad/index.js'); <--- and here for good measure
  System.import('app/main')
        .then(null, console.error.bind(console));
</script>

Now, it looks a lot like I'm shooting in the dark, and that's exactly what the error messages in the application console had me doing. 现在,看起来就像我在黑暗中拍摄,这正是应用程序控制台中的错误消息让我所做的事情。 When I try to use the module in the component like this: 当我尝试像这样在组件中使用模块时:

public elo = ELO;
constructor(){
    this.score = this.elo.expectedScore(200, 1000);
    ---- there is more to the component but this is the part where it breaks
}

I get the following message: 我收到以下消息:

"ORIGINAL EXCEPTION: TypeError: this.elo is undefined"

So the question in a broader scope is: 因此,更广泛的问题是:

How can I get a given npm package (that is not already an angular module) to work in a component or service using systemJS(or Webpack, or Browserify) as module loader in the ng2 quickstart? 在ng2快速入门中,如何使用systemJS(或Webpack或Browserify)作为模块加载器来获取给定的npm包(尚不是angular模块)在组件或服务中工作?

I have some comments here: 我在这里有一些评论:

  • You should configure SystemJS this way for your module: 您应该以这种方式为模块配置SystemJS:

     System.config({ map:{'arpad':'node_modules/arpad/index.js'} packages: { app: { format: 'register', defaultExtension: 'js' } } }); 
  • You don't need to import your index.js file (see System.import('node_modules/arpad/index.js'); ) before importing your application (importing the app/main module). 在导入应用程序(导入app/main模块)之前,不需要导入index.js文件(请参阅System.import('node_modules/arpad/index.js'); )。

  • You need to import your elo object this way: 您需要通过以下方式导入elo对象:

     import * as Elo from 'arpad'; 
  • Then you should be able to use your module this way: 然后,您应该能够以这种方式使用模块:

     constructor() { this.elo = new Elo(); this.score = this.elo.expectedScore(200, 1000); } 

Here is a plunkr describing this: https://plnkr.co/edit/K6bx97igIHpcPZqjQkkb?p=preview . 这是一个描述这个的插件: https ://plnkr.co/edit/K6bx97igIHpcPZqjQkkb?p = preview。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM