片刻时区在加载时返回Uncaught TypeError [英] Moment Timezone returning Uncaught TypeError on load

查看:84
本文介绍了片刻时区在加载时返回Uncaught TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在Django应用程序中实现Moment时区,以纠正用户从不同时区访问它的问题,并且在通过Require.js导入文件时遇到错误. moment.js,moment-timezone.js和moment-timezone-data.js都已加载,但是当我的脚本运行并尝试启动它们时,moment-timezone.js和moment-timezone-data.js会引发Uncaught TypeErrors.

I am working on implementing Moment Timezone into a Django application in order to correct for users accessing it from different time zones, and I am running into an error when importing the files through Require.js. moment.js, moment-timezone.js, and moment-timezone-data.js are all loading, but when my script runs and tries to initiate them, moment-timezone.js and moment-timezone-data.js throw Uncaught TypeErrors.

我的moment-timezone-data.js文件是从 Moment.js时区数据生成器复制粘贴的,看起来像这样(尽管有更多时区):

My moment-timezone-data.js file is copy-pasted from the Moment.js timezone data generator and looks like this (albeit with more time zones):

moment.tz.add({
    "zones": {
        "America/New_York": [
            "-4:56:2 - LMT 1883_10_18_12_3_58 -4:56:2",
            "-5 US E%sT 1920 -5",
            "-5 NYC E%sT 1942 -5",
            "-5 US E%sT 1946 -5",
            "-5 NYC E%sT 1967 -5",
            "-5 US E%sT"
        ]
    },
    "rules": {
        "US": [
            "1918 1919 2 0 8 2 0 1 D",
            "1918 1919 9 0 8 2 0 0 S",
            "1942 1942 1 9 7 2 0 1 W",
            "1945 1945 7 14 7 23 1 1 P",
            "1945 1945 8 30 7 2 0 0 S",
            "1967 2006 9 0 8 2 0 0 S",
            "1967 1973 3 0 8 2 0 1 D",
            "1974 1974 0 6 7 2 0 1 D",
            "1975 1975 1 23 7 2 0 1 D",
            "1976 1986 3 0 8 2 0 1 D",
            "1987 2006 3 1 0 2 0 1 D",
            "2007 9999 2 8 0 2 0 1 D",
            "2007 9999 10 1 0 2 0 0 S"
        ],
        "NYC": [
            "1920 1920 2 0 8 2 0 1 D",
            "1920 1920 9 0 8 2 0 0 S",
            "1921 1966 3 0 8 2 0 1 D",
            "1921 1954 8 0 8 2 0 0 S",
            "1955 1966 9 0 8 2 0 0 S"
        ]
    },
    "links": {}
});

requireConfig文件的设置如下:

The requireConfig file is set up like so:

require = {
    paths: {
        "moment": ServerInfo.generateStaticPathFor("js/ext/moment/moment-with-langs"),
        "moment-timezone": ServerInfo.generateStaticPathFor("js/ext/moment/moment-timezone"),
        "moment-timezone-data": ServerInfo.generateStaticPathFor("js/ext/moment/moment-timezone-data")
    },
    shim: {
        "moment-timezone-data": {
            "deps": ["moment-timezone"]
        }
    }
};

然后我尝试像这样初始化Moment时区:

I then try to initiate Moment Timezone like so:

define(["moment", "moment-timezone", "moment-timezone-data"], function(moment) {
    var thisMoment = moment().tz('America/New_York').startOf('day');
});

moment-timezone-data.js在第1行上引发了无法调用未定义的方法'add'的未定义类型错误"

moment-timezone-data.js throws an Uncaught TypeError of "Cannot call method 'add' of undefined" on line 1:

moment.tz.add({ ... });

moment-timezone.js在第308行引发了一个未捕获的TypeError:无法调用未定义的方法'规则'":

moment-timezone.js throws an Uncaught TypeError of "Cannot call method 'rule' of undefined" on line 308:

return [zone, zone.rule(mom, lastZone)];

推荐答案

您的define()调用仅需要moment-timezonemoment-timezone-data.本质上,moment-timezone就像是moment的直接替代品,将其扩展为提供.tz().请参考示例:

Your define() call only needs moment-timezone and moment-timezone-data. Essentially, moment-timezone acts like a drop-in replacement for moment, extending it to provide .tz(). Refer to the example:

define(["moment-timezone", "moment-timezone-data"], function (moment) {
    moment().tz("America/Los_Angeles").format();
});

此外,您无需填充时区数据.相反,在使用时区数据生成器时,只需选择"AMD"选项.

Also, you don't need to shim the timezone data. Instead, just select the "AMD" option when using the timezone data builder.

这篇关于片刻时区在加载时返回Uncaught TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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