Using moment.js in Angular 2 typescript application

后端 未结 5 2124
名媛妹妹
名媛妹妹 2021-02-20 06:21

I\'m struggling in using moment.js library inside an Angular 2 Typescript app. Even after reading the answer to this question I can\'t get it to work.

This is what I did

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-20 07:06

    When you import a module using namespace syntax to gather the exports onto a single object, as in import * as moment from 'moment', you are not importing the actually moment object which the module exports, but rather all of its members. You are losing the call signature. To resolve this, in a SystemJS + TypeScript project, specify either a value of "system" for module or a value of true for allowSyntheticDefaultImports, passing these to the TypeScript compiler, preferably via a tsconfig.json file. Then import moment like so

    import moment from 'moment';
    

    and everything will work.

提交回复
热议问题