How to use ng2-datepicker for angular 2 application

I want to include the ng2-datepicker module in my application. I tried to follow as indicated in the readme file.

npm install ng2-datepicker --save

modified systemjs.config.js file for:

.map {
 'ng2-datepicker': 'npm:ng2-datepicker',
       'moment': 'npm:moment/moment.js'
}
  'ng2-datepicker': {
          format: 'register',
        defaultExtension: 'js'
      }

The following line has been added to index.html:

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

added the following line to app.module.ts

  import { DatePicker } from 'ng2-datepicker/ng2-datepicker';
declarations: [ AppComponent ,
                DatePicker]

When loading the application, the following error appears:

Error: ReferenceError: require is not defined
        at eval (http://localhost:3000/node_modules/ng2-datepicker/ng2-datepicker.js:2:24)
        at eval (http://localhost:3000/node_modules/ng2-datepicker/ng2-datepicker.js:7:3)
        at ZoneDelegate.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:203:28)     
           Evaluating http://localhost:3000/node_modules/ng2-datepicker/ng2-datepicker.js   
           Error loading http://localhost:3000/node_modules/ng2-datepicker/ng2-datepicker.js as "ng2-datepicker/ng2-datepicker" from http://localhost:3000/app/app.module.js
+4
source share
2 answers

I can use another compiler. But in my case, this is the definition of my card:

var map = {
   ...
   'ng2-datepicker':               'app-ui/node_modules/ng2-datepicker',
   'moment':                       'app-ui/node_modules/moment',
   ... 
};

I use system.js and display without a moment. js did it for me.

+1
source

// changes to systemjs.config.js

    map: {
// our app is within the app folder
          app: 'app',
          'angular2-moment': 'npm:angular2-moment',
          'moment': 'npm:moment',
          'ng2-slimscroll': 'npm:ng2-slimscroll',
          'ng2-datepicker': 'npm:ng2-datepicker',
        }

// packages tells the System loader how to load when no filename and/or no extension
    packages: {
      'angular2-moment': {
        main: './index.js',
        defaultExtension: 'js'
      },
      'moment': {
        main: './moment.js',
        defaultExtension: 'js'
      },
      'ng2-slimscroll': {
        main: './ng2-slimscroll.js',
        defaultExtension: 'js'
      },
      'ng2-datepicker': {
        // format: 'register',
        main: './ng2-datepicker.js',
        defaultExtension: 'js'
      }

    }
0
source

Source: https://habr.com/ru/post/1656658/


All Articles