简体   繁体   English

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

[英]I am getting 'Uncaught Reference Error: moment is not defined' error. How can I fix this?

I have used this js code in my laravel project for getting moment library.我在我的 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);
            }
        }

I have called the js file using this script tag inside body section我在正文部分使用此脚本标记调用了 js 文件

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

Now getting Uncaught ReferenceError: moment is not defined in console.现在得到 Uncaught ReferenceError: moment is not defined in console。 moment.min.js is in the same directory. moment.min.js 位于同一目录中。 How can I fix this.`我该如何解决这个问题。

Try 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;
}

This is because, As the script is loaded asynchronously and before loading the script into the browser you are trying to access the moment.这是因为,由于脚本是异步加载的,并且在将脚本加载到浏览器之前,您正试图访问该时刻。 The best way of doing this is to check if the library you are using is loaded or not you can to this by adding something link 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;
}

and after you will be able to refer moment refer https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement在您可以参考之后,请参考https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到错误“未捕获的引用错误:在 HTMLAnchorElement.onclick 中未定义 jsf” - I am getting an error "Uncaught Reference Error: jsf is not defined at HTMLAnchorElement.onclick" 我收到参考错误:未定义结帐 - I am getting Reference error:checkout not defined 我如何修复错误 Uncaught ReferenceError: addEvent is not defined - How can i fix the error Uncaught ReferenceError: addEvent is not defined 未捕获的参考错误:..未定义..但我可以在错误字符串中看到变量 - Uncaught reference error: .. is not defined .. but I can see the variable in the error string 为什么会出现“ Uncaught ReferenceError:未定义userId”错误? - Why am I getting “Uncaught ReferenceError: userId is not defined” error? 为什么此主链木偶代码出现未捕获的参考错误? - Why am I getting uncaught reference error with this Backbone Marionette code? 我是 nodejs 的新手,我收到了参考错误:io is not defined ,我想不通 - I am new to nodejs and I'm getting reference error: io is not defined ,I can't figure it out 如何解决错误 window 未定义 - How can I fix getting the error window is not defined Visual Studio bundler 导致 JS Uncaught ReferenceError: Cannot access variable before initialization 错误。 有谁知道我该如何解决这个问题? - Visual Studio bundler causes JS Uncaught ReferenceError: Cannot access variable before initialization error. Anyone know how I can fix this? 如何使用javascript修复错误“未捕获的引用错误:数据未定义”? - How to fix the error "uncaught reference error: data is not defined" using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM