如何在Angular2中加载第三方库

时间:2016-11-12 19:11:22

标签: javascript angularjs node.js angular

我正在尝试在Angular 2中实现一个Datepicker。但是库没有加载,因为我得到模板解析错误'material-datepicker'不是已知元素:

我的System.config.js如下所示。

(function (global) {
  System.config({
    paths: {

      'npm:': 'node_modules/'
    },

    map: {

       app: 'app',
      '@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',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.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',
      'angular2-material-datepicker': 'npm:angular2-material-datepicker'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: '../dist/main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      'angular2-material-datepicker': {
         main: 'index.js',
         defaultExtension: 'js',
      }
    }
  });
})(this);

1 个答案:

答案 0 :(得分:0)

将模块导入AppComponent以及您使用它的任何组件。

import { DatepickerModule } from 'angular2-material-datepicker'

@NgModule({
  imports: [ DatepickerModule ],
  declarations: [ ... ],
  bootstrap: [ ... ]
})