Importing moment into Angular gives error

前端 未结 3 1843
独厮守ぢ
独厮守ぢ 2021-01-13 09:12

I am getting following error: Module \'\".../node_modules/moment/moment\"\' has no exported member \'default\'when I use

import * as _moment from

相关标签:
3条回答
  • That's because moment does not have something called default did you mean defaultFormat?

    Also you don't need your second import. You can just say _moment.defaultFormat

    0 讨论(0)
  • 2021-01-13 09:59

    I had so much trouble with trying to properly import Moment that I switched to the date-fns library instead.

    date-fns takes a somewhat different approach - as the name implies, it's a collection of date functions, rather than a huge 'god object' that does everything.

    The additional benefits are that a) you can import just the individual functions that you need, and b) if you import the whole thing, it tree-shakes really well. So you wind up with a much smaller impact on your module size.

    0 讨论(0)
  • 2021-01-13 10:03

    From Moment's Docs:

    Note: If you have trouble importing moment, try adding "allowSyntheticDefaultImports": true in compilerOptions in your tsconfig.json file and then use the syntax

    0 讨论(0)
提交回复
热议问题