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

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

问题描述

我刚刚在我的Ember.js应用程序中将moment@2.1.0更新为moment@2.7.0,现在我在进入控制台或将其包含在我的脚本。



在我看到的文档中自2.4.0以来,全局导出的时刻对象已被弃用。我认为这里是一个问题,但我不确定如何在全球范围内设置ember,以便我们的代码库中的所有对moment()的引用目前都不会被破坏。


$ b $我尝试更新我们正在做的需求('moment')的行到var moment = require('moment'),但它具有相同的结果。即使我把调试器直接放在var moment = ...之后,然后在控制台中输入moment(),我还是得不到那一刻。

解决方案

您希望新文档的浏览器部分。



所以...

 < script src =moment.js >< /脚本> 
< script>
moment()。format();
< / script>

编辑



说完了,看起来可能会给你同样的问题。



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



请阅读 CommonJS AMD ,并尝试将其纳入您当前的堆栈。



您应该执行以下操作:

  var moment = require('moment'); 

moment()。format();

但是为了使用 require 一个如上所述的模块系统。



但是



明显的修复是回滚您的Moment.js版本,只要您不需要新功能即可。一个简单的时间/日期库更新的重要性可能是多少?


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.

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.

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.

解决方案

You want the Browser section of the new Documentation.

So...

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

Edit

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

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.

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

You should be doing something like:

var moment = require('moment');

moment().format();

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

But

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天全站免登陆