简体   繁体   English

使用npm angular2导入库

[英]Import library using npm angular2

I'm not able to setup this library using npm. 我无法使用npm设置此库

Commands done : 完成的命令:

npm install ng2-charts --save
npm install chart.js --save

index.html 的index.html

I added this line 我加了这条线

        <script src="node_modules/chart.js/src/chart.js"></script>

app.module app.module

        import { ChartsModule } from 'ng2-charts/ng2-charts';

    @NgModule({
      imports: [
        BrowserModule,
        RouterModule.forRoot(router),
        HttpModule,
        ChartsModule,

      ],

systemjs.config.js systemjs.config.js

(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      'app': 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'chartJS':                   'npm:ng2-charts'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        defaultExtension: 'js',
        meta: {
          './*.js': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      chartJS:{
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

My console 我的控制台

控制台错误

The content of this localhost:3005/ng2-charts/ng2-charts is in fact an HTML file - it refers to my index.html . 这个localhost:3005/ng2-charts/ng2-charts内容实际上是一个HTML文件-它引用了我的index.html

I'm not able to understand how it's working for hours now. 我现在几个小时都无法理解它的工作方式。

Your system.js config should be, 您的system.js配置应该是

  map: {
    'chart.js': 'npm:chart.js/dist/Chart.bundle.min.js',
     'ng2-charts': 'npm:ng2-charts'
  }

and in packages, 在包装中

 'ng2-charts': {
   main: 'ng2-charts.js',
   defaultExtension: 'js'
  }

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

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