[英]Importing moment-timzone and moment-range with webpack (Babel/ES6)
I'm using Webpack with Babel loader. 我正在使用带有Babel加载器的Webpack。 Writing by ES6 standards.
按ES6标准编写。 I have installed both moment-timezone and moment-range with npm.
我已经用npm安装了时刻 - 时区和时刻 - 范围。
Both of these are moment.js plugins, and both of these depend on the moment package, and export a separate moment library. 这两个都是moment.js插件,这两个都取决于当前的包,并导出一个单独的时刻库。 So when I do
所以,当我这样做
import moment from 'moment-timezone';
import moment2 from 'moment-range';
then I get two separate references to moment . 然后我得到两个单独的参考时刻 。
How can I set it up so I could use moment with timezone and range functions? 我该如何设定,让我可以使用瞬间与时区和范围的功能呢?
Thanks! 谢谢!
Docs show CommonJS syntax for this: 文档为此显示了CommonJS语法:
var moment = require('moment');
require('moment-range');
require('moment-timezone');
// moment() now has both range and timezone plugin features
In ES6: 在ES6中:
import moment from 'moment';
import 'moment-range';
import 'moment-timezone';
EDIT 编辑
Since moment-timezone
does not extend existing import but does extend moment
itself, what about this? 由于
moment-timezone
并没有扩展现有的导入,但确实延长了moment
本身,那么这个呢?
import moment from 'moment-timezone';
import 'moment-range';
I had myself this problem with different packages: moment-timezone
and frozen-moment
. 我用不同的包装解决了这个问题:
moment-timezone
和frozen-moment
。 The root of all evil is having two moment
dependencies in different parts of the tree. 所有邪恶的根源是在树的不同部分有两个
moment
依赖。 In my case, I had moment
right under node_modules
and also inside of frozen-moment
's node-modules
. 就我而言,我不得不
moment
右下node_modules
也内frozen-moment
的node-modules
。 This meant that moment-timezone
and frozen-moment
were using different moment
instances. 这意味着
moment-timezone
和frozen-moment
正在使用不同的moment
实例。
Check that you are using versions of packages that are compatible with each other so that moment-range
doesn't need to fetch a different version of moment
. 检查您是否使用彼此兼容的软件包版本,以便
moment-range
不需要获取不同版本的moment
。 When you have it correctly you should be able to do this: 如果你有正确的,你应该能够这样做:
import moment from 'moment';
import momentTimezone from 'moment-timezone';
import momentRange from 'moment-range';
console.log(moment === momentTimezone === momentRange); // logs 'true'
Keep in mind that's only for testing it's properly setup, you should use it like in glortho's answer: 请记住,仅用于测试它的正确设置,您应该像在glortho的答案中那样使用它:
import moment from 'moment';
import 'moment-timezone';
import 'moment-range';
All the above solutions did not work for me and I had to resort to this: 所有上述解决方案对我都不起作用,我不得不求助于此:
import moment from 'moment';
window.moment = moment;
import {extendMoment} from 'moment-range';
extendMoment(window.moment);
A little hacky but works. 有点hacky但有效。
I find the default implementation of moment-range pretty ugly, since it just extends the moment object, and IMO that's kinda "hacky". 我发现时刻范围的默认实现非常难看,因为它只是扩展了时刻对象,而IMO则有点“hacky”。
This is the way I'm doing it: 这就是我这样做的方式:
import { default as DateRange } from 'moment-range';
const myRange = new DateRange(x, y);
I managed to import moment-timezone
and moment-range
with this code: 我设法用以下代码导入
moment-timezone
和moment-range
:
import Moment from 'moment-timezone';
import momentRange from 'moment-range';
const moment = momentRange.extendMoment(Moment);
My project uses webpack with ES6 (same as OP I think), with React. 我的项目使用带有ES6的webpack(与我认为的OP相同)和React。 I need functionality from moment timezone.
我需要从时刻到时区的功能。 After adding both
moment
and moment-timezone
npm dependencies, this import statement works: 添加
moment
和moment-timezone
npm依赖项后,此import语句有效:
import moment from 'moment-timezone';
That allows me to make calls like moment.tz.guess();
这让我可以像
moment.tz.guess();
What really threw me off though- just adding that import doesn't make moment
available in my browser's dev console. 什么真的让我失望 - 只是添加导入并没有在我的浏览器的开发控制台中提供
moment
。 Going through the comments in https://github.com/moment/moment/issues/2608 , someone mentioned this snippet which makes moment
available in the dev console: 通过在评论中去https://github.com/moment/moment/issues/2608 ,有人提到了这个片段,这使得
moment
可供开发控制台:
window.moment = moment;
This worked for me 这对我有用
import * as moment from 'moment-timezone'
import * as momentRange from 'moment-range'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.