<p>moment 没有默认成员</p>

创建于 2018-01-09  ·  4评论  ·  资料来源: moment/moment

问题描述和重现步骤:
尝试在打字稿项目中实现实例语言环境
https://momentjs.com/docs/#/i18n/instance -locale/
但是当我import moment from 'moment'我得到错误:
moment has no default export

目的:
在应用程序的某些部分,我需要自定义形式的人性化字符串(缩短,如“秒”而不是“秒”),因此我想在一个视图中使用两个语言环境。

环境:
打字稿项目

其他可能有用的信息:
打字稿,Angular4

如果您要报告问题,请在您使用的环境中运行以下代码并包含输出:

console.log( (new Date()).toString())
console.log((new Date()).toLocaleString())
console.log( (new Date()).getTimezoneOffset())
console.log( navigator.userAgent)
console.log(moment.version)
Tue Jan 09 2018 15:46:46 GMT+0100 (CET)
VM12667:2 09/01/2018, 15:46:46
VM12667:3 -60
VM12667:4 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
VM12667:5 Uncaught ReferenceError: moment is not defined
    at <anonymous>:5:13



md5-1f8dcc13f1f8244065e29a6dcf5a9efa



import * as moment from 'moment';
console.log('version', moment.version);



md5-1f8dcc13f1f8244065e29a6dcf5a9efa



version 2.20.1

我想阻止import * as moment from 'moment';解决方案,因为它阻止了捆绑中的三个摇晃

非常感谢! :)

最有用的评论

转播😂

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

https://momentjs.com/docs/#/use -it/typescript/

所有4条评论

转播😂

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

https://momentjs.com/docs/#/use -it/typescript/

是否有可能以不再需要allowSyntheticDefaultImport的方式解决此问题? 要求它的问题在于它要求每个使用 moment 的人也有这个设置。

通过不需要此设置,您可以轻松地将moment用于对allowSyntheticDefaultImport有自己偏好的项目中,但现在该设置实际上是强加给每个人的。

只是在这里插一句,这是一个非常、真正、令人讨厌的问题。 由于 moment 选择不尊重官方规范,这意味着一些库通过import * as moment from "moment"引入,而其他库通过import defaultMoment from "moment" 。 因此,当我的项目尝试加载两者时,我只是无法获得正确的设置来获得汇总以正确编译它。 我已经尝试了几乎所有allowSyntheticDefaultImports : trueesModuleInterop : truemoduleResolution : "node"skipLibCheck : true的排列和组合,但我无法得到一个正常工作的排列。 一种解决方案将允许具有一种导入格式的库工作,但在另一种中失败。

规范存在是有原因的,这是为了创造一致性。 那一刻有没有办法可以有一个 moment-es 变体或可以正确导入的东西,这样想要遵循规范的人就可以遵循规范,让 JS 工具链按预期工作,而不是所有的黑客?

+1

此页面是否有帮助?
0 / 5 - 0 等级