如何使用 Aurelia/Typescript 导入 Moment-Timezone [英] How to import Moment-Timezone with Aurelia/Typescript

查看:26
本文介绍了如何使用 Aurelia/Typescript 导入 Moment-Timezone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经正确导入了 momentjs.它工作正常,但是当我尝试导入时刻时区时,我无法让它工作.我无权使用任何功能.

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.

这是我从 npm 加载它们的 aurelia.json 文件:

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.

现在,当我尝试指定时区时,我收到Moment Timezone has no data for 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

安装moment-timezone

  • 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的dependencies"部分配置:

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 Timezone 没有欧洲/阿姆斯特丹的数据.参见 http://momentjs.com/timezone/docs/#/data-loading/>

Moment Timezone has no data for Europe/Amsterdam. See 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆