【问题标题】:I am getting 'Uncaught Reference Error: moment is not defined' error. How can I fix this?我收到“未捕获的引用错误:未定义时刻”错误。我怎样才能解决这个问题?
【发布时间】:2020-11-26 07:07:00
【问题描述】:

我在我的 laravel 项目中使用了这个 js 代码来获取矩库。

        var today = moment();

        function Calendar(selector, events) {
            this.el = document.querySelector(selector);
            this.events = events;
            this.current = moment().date(1);
            this.draw();
            var current = document.querySelector('.today');
            if (current) {
                var self = this;
                window.setTimeout(function () {
                    self.openDay(current);
                }, 500);
            }
        }

我已经在正文部分使用这个脚本标签调用了 js 文件

<script src="moment.min.js"></script>

现在出现 Uncaught ReferenceError: moment is not defined in console。 moment.min.js 位于同一目录中。 我该如何解决这个问题。`

【问题讨论】:

标签: javascript php html laravel


【解决方案1】:

试试这个

function loadError(oError) {
  throw new URIError("The script " + oError.target.src + " didn't load 
correctly.");
}

function affixScriptToHead(url, onloadFunction) {
  var newScript = document.createElement("script");
  newScript.onerror = loadError;
  if (onloadFunction) { newScript.onload = onloadFunction; }
  document.head.appendChild(newScript);
  newScript.src = url;
}

【讨论】:

    【解决方案2】:

    这是因为,由于脚本是异步加载的,并且在将脚本加载到浏览器之前,您正试图访问该时刻。这样做的最好方法是检查您正在使用的库是否已加载,您可以通过添加一些链接 this

    function loadError(oError) {
      throw new URIError("The script " + oError.target.src + " didn't load correctly.");
    }
    
    function affixScriptToHead(url, onloadFunction) {
      var newScript = document.createElement("script");
      newScript.onerror = loadError;
      if (onloadFunction) { newScript.onload = onloadFunction; }
      document.head.appendChild(newScript);
      newScript.src = url;
    }
    

    之后您将能够参考时刻参考https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement

    【讨论】:

      猜你喜欢
      • 2012-03-16
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多