我收到“未捕获参考错误:时刻未定义”错误。我怎样才能解决这个问题?

我在我的 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 在同一目录中。我该如何解决这个问题。`


冉冉说
浏览 125回答 2
2回答

宝慕林4294392

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

德玛西亚99

这是因为,由于脚本是异步加载的,并且在将脚本加载到浏览器之前,您正在尝试访问这一刻。这样做的最好方法是检查您正在使用的库是否已加载,您可以通过添加一些链接来做到这一点function loadError(oError) {&nbsp; throw new URIError("The script " + oError.target.src + " didn't load correctly.");}function affixScriptToHead(url, onloadFunction) {&nbsp; var newScript = document.createElement("script");&nbsp; newScript.onerror = loadError;&nbsp; if (onloadFunction) { newScript.onload = onloadFunction; }&nbsp; document.head.appendChild(newScript);&nbsp; newScript.src = url;}在您可以参考 moment 之后参考https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript