我有一个基于 AngularJS 的 SPA,我正在尝试在 AngularJS Controller 中获取带有时刻时区的时区名称列表。
- 这是我导入时遇到的错误:“Uncaught TypeError: Cannot read property 'length' of undefined. at addZone (moment-timezone.js:385)”
- MomentJS 本身工作得很好,即导入时刻(不是时刻时区)
- 我更喜欢使用纯 Moment,而不是像 Angular 矩这样的任何环绕方式
- 在 Chrome 浏览器上运行
为什么会发生这个错误? 您能提供一个有效的小代码示例吗?
更新(我正在添加代码):
'use strict';
import moment from 'moment'; // works
import momentTZ from 'moment-timezone'; // this breaks it
export default class RequestCtrl {
constructor() {
console.log(moment()); //works
//console.log(momentTZ.tz.names());
//console.log(moment().tz('America/Los_Angeles').format());}
为了单独测试时刻时区,我使用了 JSFiddle。这是 TZ 使用 angularJS 的屏幕截图。不幸的是,它仍然在我的项目中出现问题
最佳答案
如果您希望获取所有时区的列表,则必须导入 moment-timezone
模块。
您可以像我们处理其他模块一样导入和使用 moment-timezone
模块。
其中我们可以使用 moment.tz.names()
获取所有时区列表
确保您的 webpack 配置中有 json loader。看起来时刻时区模块需要这个。
关于javascript - 在 AngularJS Controller 中获取带有 moment-timezone 的时区名称列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48847179/