如何使用多个时刻插件? [英] How to use multiple moment plugins?
问题描述
我知道如果我想使用它,我必须导入片刻插件。像这样:
I know I have to import the moment plugin if I want to use it. Like this:
import * as moment from "moment-timezone";
//here I can use moment.tr.names() etc.
<但是,如果我想使用多个插件怎么办?我知道插件在瞬间导入时刻,添加其功能然后再次导出时刻。但如果这是正确的,我如何使用多个插件?
But what if I want to use multiple plugins? I know that plugins in moment import moment, add their functionality and then export moment again. But if that's correct, how do I use multiple plugins?
一个选项是以不同的名称导入它们,如:
One option is to import them under a different name like:
import * as momentJdate from "moment-jdateformatparser";
import * as momentTimezone from "moment-timezone";
或者我可以将它们合并为这样的一个时刻对象(使用deepExtend):
Or I can merge them into one moment object like this (using deepExtend):
let moment = {};
deepExtend(moment, momentJdate, momentTimezone);
//here you should be able to use moment().toJDFString() and moment.tz.names()
但这些似乎都不是干净的代码。有没有更好的方法呢?
But neither of those seem to be clean code. Is there a better way to do this?
推荐答案
什么对我有用,我认为是干净的其他。例如:
What works for me and I think is clean-ish to import one after the other. For example:
moment = require('moment-business-days');
moment = require('moment-timezone');
moment().tz('America/New_York').businessAdd(1).startOf('day').add(7, 'hours')
这是不理想的,因为它是不透明的,如果不了解节点的工作原理,你可能会认为第一个要求是没有意义的。但它干净而有效。
It is not ideal, because it is opaque and without understanding how node works you might think the first require is pointless. But it is clean and effective.
这篇关于如何使用多个时刻插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!