moment.js 时刻未定义 [英] moment.js moment is undefined

查看:41
本文介绍了moment.js 时刻未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 Ember.js 应用程序中将 moment@2.1.0 更新为 moment@2.7.0,现在我收到一个错误,当我在控制台中输入 moment() 或将其包含在我的脚本.

I just updated form moment@2.1.0 to moment@2.7.0 in my Ember.js application and now I am getting an error the moment is not defined when I enter moment() into the console or include it in my scripts.

在文档中,我看到自 2.4.0 起,不推荐使用全局导出的矩对象."我认为这是这里的问题,但我不确定如何在 ember 中全局设置 moment,以便我们的代码库中对 moment() 的所有引用当前都不会被破坏.

On the docs I saw "Since 2.4.0 the globally exported moment object is deprecated." which I think is the issue here but I am unsure how I can set moment globally in ember so that all of the references to moment() in our code base currently aren't broken.

我尝试将我们执行 require('moment') 的行更新为 var moment = require('moment') 但结果相同.即使我在 var moment = ... 行之后直接放置一个调试器,然后在控制台中输入 moment() 我仍然得到那个时刻未定义.

I have tried updating the line where we were doing require('moment') to var moment = require('moment') but it has the same result. Even if I put a debugger directly after the var moment = ... line and then enter moment() into the console I still get that moment is undefined.

推荐答案

您想要浏览器新文档的部分.

所以...

<script src="moment.js"></script>
<script>
    moment().format();
</script>

编辑

话虽如此,看起来这可能会给您带来同样的问题.

Having said that, it looks like this will probably give you the same issue.

看起来 Moment.js 试图鼓励您使用包管理器,以免用全局变量弄脏命名空间.

It looks like Moment.js is trying to encourage you to use a package manager in order not to dirty the namespace with global variables.

阅读 CommonJSAMD,并尝试将其纳入您当前的堆栈.

Go read up on CommonJS and AMD, and try and factor it in to your current stack.

你应该这样做:

var moment = require('moment');

moment().format();

但是为了使用require,你需要一个像前面提到的模块系统.

But in order to use require you need a module system like the ones aforementioned.

但是

显而易见的解决方法是回滚您的 Moment.js 版本,只要您不需要新功能即可.一个简单的时间/日期库的更新到底有多重要?

The obvious fix is to rollback your version of Moment.js, so long as you don't need new functionality. How important can an update on a simple time/date library really be?

这篇关于moment.js 时刻未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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