Using Rollup for Angular 2's AoT compiler and importing Moment.js

后端 未结 9 1738
天涯浪人
天涯浪人 2020-12-24 13:34

I\'m trying to follow the official AoT guide for Angular 2, and I\'m using Moment.js in my application. Moment.js is on my packages.json file, and I\'m using versio

9条回答
  •  庸人自扰
    2020-12-24 13:43

    Here is what I did to make work moment with typescript (at 2.1.6) and rollup (0.41.4).

    1. To import moment, keep the standard way:

      import * as moment from 'moment';

    import moment from 'moment'; is non-standard for a package without a default export, it will result an error at runtime: moment_1.default is not a function

    1. In typescript use moment with by casting moment as any, and call the default function:

      var momentFunc = (moment as any).default ? (moment as any).default : moment;
      var newFormat = momentFunc(value).format( format );
      

    moment(value).format(format) will result an error at rollup tree shaking: Cannot call a namespace ('moment')

提交回复
热议问题