Moment.js:Uncaught TypeError:无法在moment.js读取未定义的属性'defineLocale':13

时间:2017-06-17 00:48:14

标签: javascript momentjs libraries

当运行下面的小html文件时,我看到以下控制台日志错误:

moment.js:13 Uncaught TypeError: Cannot read property 'defineLocale' of undefined
    at moment.js:13
    at moment.js:9
    at moment.js:10

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>JS Bin</title>

    <script src="../scripts/libraries/moment.js"></script>

</head>

<body>


<script>
  var now = moment()
  console.log(now);
</script>

</body>
</html>

我还尝试使用此CDN链接替换对本地库的引用:https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/af.js

任何人都知道这个错误是什么?

2 个答案:

答案 0 :(得分:20)

您的moment.js版本似乎存在问题。您拥有的脚本仅适用于locale,您需要包含moment.js脚本:

https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
</head>

<body>
  <script>
    var now = moment()
    console.log(typeof moment.defineLocale)
  </script>
</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

当您在js时刻之前包含区域设置js时,也会发生此错误。

例如以下给出错误:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/locale/tr.js"></script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

要纠正,只需将上面的一刻js放在语言环境js中。