Vue 模块,moment-timezone - 如何正确加载 moment-timezone 以及如何使用 2012-2022 数据 [英] Vue modules, moment-timezone - How to load moment-timezone correctly and how to use the 2012-2022 data

查看:134
本文介绍了Vue 模块,moment-timezone - 如何正确加载 moment-timezone 以及如何使用 2012-2022 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 vue-cli 中的 webpack 模板用于我的 Vue 项目,并且我在 npm 中安装了 moment 和 moment-timezone.

I'm using the webpack template from vue-cli for my Vue project and I have both moment and moment-timezone installed in npm.

在我的单文件组件中,我已经能够通过包含以下导入行来使用 moment-timezone:

In my single file components, I've been able to get moment to work with moment-timezone by including the following import lines:

import moment from 'moment'
import moment_timezone from 'moment-timezone'

moment_timezone 对象从未使用过,但它似乎需要存在才能使 moment.tz 函数工作.

The moment_timezone object isn't ever used, but it seems to need to be there for moment.tz functions to work.

我的问题是:

  1. 这是在我的设置中加载时刻和时刻时区的正确方法吗?
  2. 如何仅加载来自 moment-timezone-with-data-2012-2022.min.js 的数据?我在 moment-timezone 包的 builds 目录中看到它,但我不清楚如何指定这是我想要使用的数据.

推荐答案

回答我自己的问题,因为我似乎已经把它整理好了.

Answering my own question as I seem to have sorted it out.

1 - 似乎moment-timezone 需要使用moment,所以如果你要同时使用moment 和moment-timezone,你可以简单地从moment-timezone 模块导入moment 和它会工作得很好.所以...

1 - It seems that moment-timezone requires the use of moment, so if you are going to use both moment and moment-timezone, you can simply import moment from the moment-timezone module and it will work just fine. So...

import moment from 'moment'
import moment_timezone from 'moment-timezone'

变成

import moment from 'moment-timezone'

2 - Moment-timezone 包含表示完整的 moment-timezone 代码以及 data/builds 下数据的特定版本的文件.您可以直接从这些文件中导入 moment-timezone,而无需在模块的根目录中导入 moment-timezone.js 文件.所以...

2 - Moment-timezone has files that represent the complete moment-timezone code plus specific versions of the data under data/builds. You can import moment-timezone directly from these files without needing to import the moment-timezone.js file in the root of the module. So...

import moment from 'moment-timezone'

变成

import moment from 'moment-timezone/data/builds/moment-timezone-with-data-2012-2022.js'

这篇关于Vue 模块,moment-timezone - 如何正确加载 moment-timezone 以及如何使用 2012-2022 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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