如何使用Aurelia/Typescript导入Moment-Timezone [英] How to import Moment-Timezone with Aurelia/Typescript
问题描述
我已经正确导入了momentjs.它工作正常,但是当我尝试导入moment-timezone时,我无法使其正常工作.我无权使用任何功能.
I've imported momentjs properly. It's working fine, but when I go to try to import moment-timezone, I can't get it to work. I don't have access to any functions.
这是我的aurelia.json文件,我正在从npm加载它们:
Here's my aurelia.json file where I'm loading them from npm:
{
"name": "moment",
"path": "../node_modules/moment",
"main": "moment",
"exports": "moment"
},
{
"name": "moment-timezone",
"path": "../node_modules/moment-timezone",
"main": "moment-timezone",
"deps": [ "moment" ],
"exports": "tz"
}
,这是我尝试将它们加载到文件中的位置:
and here's where I'm trying to load them into the file:
import { autoinject } from "aurelia-framework";
import * as moment from "moment";
import * as tz from "moment-timezone";
@autoinject
export class GlobalUtil {
}
这是加载它们的正确方法吗?不幸的是,它对我不起作用.
Is this the right way to load them? It's not working for me unfortunately.
现在,当我尝试指定时区时,出现矩时区没有America/New_York的数据".然后是console.logs(7am),这不是正确的时间.
Now when I try to specify a timezone, I get a "Moment Timezone has no data for America/New_York." Then it console.logs(7am), which is not the correct time.
推荐答案
也许我可以帮上忙.为了给出合理的说明,我将为在Aurelia CLI应用程序中添加动量/动量时区的整个链添加分步说明.
Maybe I can help a bit with this. In order to give decent instructions, I'll add step-by-step instructions for the entire chain of adding moment/moment-timezone to an Aurelia CLI app.
安装moment.js
-
npm install --save moment
-
typings install dt~moment --global --save
npm install --save moment
typings install dt~moment --global --save
安装时刻-时区
-
npm install --save moment-timezone
-
typings install dt~moment-timezone --global --save
npm install --save moment-timezone
typings install dt~moment-timezone --global --save
注意:我假设您要使用TypeScript.如果没有,请跳过以上安装说明中的第二步.
Note: I'm assuming you want to use TypeScript. If not, skip the second steps from the installation instructions above.
接下来,在 aurelia.json 的依赖项"部分中进行配置:
Next, configure both in the "dependencies" section of aurelia.json:
{
"name": "moment",
"path": "../node_modules/moment",
"main": "moment"
},
{
"name": "moment-timezone",
"path": "../node_modules/moment-timezone",
"main": "moment-timezone",
"deps": ["moment"]
}
最后,让我们用一个简单的示例来总结一下如何实际使用它:
Finally, let's wrap this up with a simple example on how to actually use this:
import * as moment from 'moment';
import 'moment-timezone';
export class App {
message: string;
constructor() {
// basic example of using moment().tz
this.message = moment.tz("2014-06-01 12:00", "Europe/Amsterdam").format();
}
}
应该的!到目前为止,至少对于配置部分来说如此.但是,正如您的问题已经表明的那样,您可能会收到以下趋势的错误:
That should be it! So far so good, at least for the configuration part. But, as your question already indicates, you may receive an error in the trend of:
Moment时区没有欧洲/阿姆斯特丹的数据.请参见 http://momentjs.com/timezone/docs/#/data-loading/ >
这意味着,默认情况下,moment-timezone不了解该特定时区.如错误消息中的链接所述,您必须自己添加它.例如:
This means that, by default, moment-timezone has no knowledge of that specific timezone. As the link from the error message mentions, you have to add it yourself. For example like:
moment.tz.add([
'America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0',
'Europe/Amsterdam|EST EDT|50 40|0101|1Lz50 1zb0 Op0'
]);
为此数组添加任意数量的时区.或从momentjs网站下载/使用预定义的捆绑包(请参阅上面的链接).
Add as much timezones as you'd like to this array. Or download/use a predefined bundle from the momentjs website (see link above).
希望这会有所帮助!
这篇关于如何使用Aurelia/Typescript导入Moment-Timezone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!